Skip to content

Commit

Permalink
compiling version
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Jul 27, 2020
1 parent 42766a1 commit 1591bc6
Show file tree
Hide file tree
Showing 20 changed files with 828 additions and 267 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"chart.js": "^3.0.0-alpha.2"
},
"dependencies": {
"@types/d3-geo": "^1.11.1",
"@types/d3-scale-chromatic": "^1.5.0",
"@types/topojson-client": "^3.0.0",
"d3-geo": "^1.12.1",
"d3-scale-chromatic": "^1.5.0",
"topojson-client": "^3.1.0"
Expand Down Expand Up @@ -106,5 +109,8 @@
"prepare": "npm run build",
"release": "release-it --disable-metrics --npm.skipChecks",
"release:pre": "release-it --disable-metrics --npm.skipChecks --preRelease=alpha --npm.tag=next"
},
"resolutions": {
"@sgratzl/chartjs-esm-facade": "portal:/D:/w/chartjs/chartjs-esm-facade"
}
}
2 changes: 1 addition & 1 deletion samples/albers.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion samples/albersLog.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion samples/bubbleMap.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<!-- <script src="https://unpkg.com/chartjs-plugin-datalabels"></script> -->
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
<script src="https://unpkg.com/papaparse"></script>
</head>

Expand Down
2 changes: 1 addition & 1 deletion samples/bubbleMapArea.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<!-- <script src="https://unpkg.com/chartjs-plugin-datalabels"></script> -->
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
<script src="https://unpkg.com/papaparse"></script>
</head>

Expand Down
2 changes: 1 addition & 1 deletion samples/bubbleMapLog.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<!-- <script src="https://unpkg.com/chartjs-plugin-datalabels"></script> -->
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
<script src="https://unpkg.com/papaparse"></script>
</head>

Expand Down
2 changes: 1 addition & 1 deletion samples/earth.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion samples/earth_click.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion samples/germany.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script src="https://unpkg.com/chart.js@3.0.0-alpha.2/dist/chart.js"></script>
<script src="https://unpkg.com/@sgratzl/chartjs-esm-facade@3.0.0-alpha.2"></script>
<script src="../build/Chart.Geo.js"></script>
<script src="../dist/chartgeo.umd.development.js"></script>
</head>

<body>
Expand Down
160 changes: 102 additions & 58 deletions src/controllers/bubbleMap.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,162 @@
import { Chart, Point, BubbleController, merge } from '@sgratzl/chartjs-esm-facade';
import { geoDefaults, GeoController } from './geo';
import {
Chart,
Point,
BubbleController,
merge,
UpdateMode,
ITooltipItem,
IScriptableContext,
ChartItem,
IControllerDatasetOptions,
ScriptableAndArrayOptions,
ICommonHoverOptions,
IChartDataset,
IChartConfiguration,
IPointOptions,
IPointProps,
} from '@sgratzl/chartjs-esm-facade';
import { geoDefaults, GeoController, IGeoChartOptions } from './geo';
import { SizeScale, ProjectionScale } from '../scales';
import { GeoFeature } from '../elements';
import { GeoFeature, IGeoFeatureOptions } from '../elements';
import patchController from './patchController';

export class BubbleMapController extends GeoController {
export class BubbleMapController extends GeoController<Point> {
linkScales() {
super.linkScales();
const dataset = this.getDataset();
const dataset = this.getGeoDataset();
const meta = this.getMeta();
meta.vAxisID = meta.rAxisID = 'r';
dataset.vAxisID = dataset.rAxisID = 'r';
meta.rScale = this.getScaleForId('r');
meta.vScale = meta.rScale;
meta.iScale = meta.xScale;
meta.iAxisID = dataset.iAxisID = meta.xAxisID;
meta.iAxisID = dataset.iAxisID = meta.xAxisID!;
}

parse(start, count) {
const rScale = this.getMeta().rScale;
const data = this.getDataset().data;
parse(start: number, count: number) {
const rScale = this.getMeta().rScale!;
const data = (this.getDataset().data as unknown) as IBubbleMapPoint[];
const meta = this._cachedMeta;
for (let i = start; i < start + count; ++i) {
const d = data[i];
meta._parsed[i] = {
x: d.longitude == null ? d.x : d.longitude,
y: d.latitude == null ? d.y : d.latitude,
[rScale.axis]: rScale.parse(d),
[rScale.axis]: rScale.parse(d, i),
};
}
}

updateElements(elems, start, mode) {
updateElements(elems: Point[], start: number, mode: UpdateMode) {
const reset = mode === 'reset';
const firstOpts = this.resolveDataElementOptions(start, mode);
const sharedOptions = this.getSharedOptions(mode, elems[start], firstOpts);
const includeOptions = this.includeOptions(mode, sharedOptions);
const scale = this.getProjectionScale();

this.getMeta().rScale._model = firstOpts; // for legend rendering styling
(this.getMeta().rScale! as SizeScale)._model = firstOpts; // for legend rendering styling

for (let i = 0; i < elems.length; i++) {
const index = start + i;
const elem = elems[i];
const parsed = this.getParsed(i);
const xy = scale.projection([parsed.x, parsed.y]);
const properties = {
const xy = scale.projection!([parsed.x, parsed.y]);
const properties: IPointProps & { options?: IPointOptions; skip: boolean } = {
x: xy ? xy[0] : 0,
y: xy ? xy[1] : 0,
skip: Number.isNaN(parsed.x) || Number.isNaN(parsed.y),
};
if (includeOptions) {
properties.options = this.resolveDataElementOptions(index, mode);
if (reset) {
properties.options.radius = 0;
properties.options!.radius = 0;
}
}
this.updateElement(elem, index, properties, mode);
}
this.updateSharedOptions(sharedOptions, mode);
}

indexToRadius(index) {
const rScale = this.getMeta().rScale;
indexToRadius(index: number) {
const rScale = this.getMeta().rScale as SizeScale;
return rScale.getSizeForValue(this.getParsed(index)[rScale.axis]);
}
}

BubbleMapController.id = 'bubbleMap';
BubbleMapController.defaults = merge({}, [
geoDefaults,
{
dataElementType: Point.id,
dataElementOptions: BubbleController.defaults.dataElementOptions,
datasetElementType: GeoFeature.id,
showOutline: true,
clipMap: 'outline+graticule',
tooltips: {
callbacks: {
title() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
},
label(item) {
if (item.formattedValue == null) {
return item.chart.data.labels[item.dataIndex];
}
return `${item.chart.data.labels[item.dataIndex]}: ${item.formattedValue}`;
static id = 'bubbleMap';
static defaults: any = merge({}, [
geoDefaults,
{
dataElementType: Point.id,
dataElementOptions: BubbleController.defaults.dataElementOptions,
datasetElementType: GeoFeature.id,
showOutline: true,
clipMap: 'outline+graticule',
tooltips: {
callbacks: {
title() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
},
label(item: ITooltipItem) {
if (item.formattedValue == null) {
return item.chart.data.labels[item.dataIndex];
}
return `${item.chart.data.labels[item.dataIndex]}: ${item.formattedValue}`;
},
},
},
},
scales: {
r: {
type: SizeScale.id,
scales: {
r: {
type: SizeScale.id,
},
},
},
elements: {
point: {
radius(context) {
if (context.dataIndex == null) {
return null;
}
const controller = context.chart.getDatasetMeta(context.datasetIndex).controller;
return controller.indexToRadius(context.dataIndex);
elements: {
point: {
radius(context: IScriptableContext) {
if (context.dataIndex == null) {
return null;
}
const controller = context.chart.getDatasetMeta(context.datasetIndex).controller as BubbleMapController;
return controller.indexToRadius(context.dataIndex);
},
hoverRadius: undefined,
},
hoverRadius: undefined,
},
},
},
]);
]);
}

interface IBubbleMapPoint {
longitude: number;
latitude: number;
x?: number;
y?: number;
value: number;
}

export interface IBubbleMapControllerDatasetOptions
extends IControllerDatasetOptions,
IGeoChartOptions,
ScriptableAndArrayOptions<IGeoFeatureOptions>,
ScriptableAndArrayOptions<ICommonHoverOptions> {}

export type IBubbleMapControllerDataset<T = IBubbleMapPoint> = IChartDataset<T, IBubbleMapControllerDatasetOptions>;

export type IBubbleMapControllerConfiguration<T = IBubbleMapPoint, L = string> = IChartConfiguration<
'bubbleMap',
T,
L,
IBubbleMapControllerDataset<T>
>;

export class BubbleMapChart<T = IBubbleMapPoint, L = string> extends Chart<
T,
L,
IBubbleMapControllerConfiguration<T, L>
> {
static id = BubbleMapController.id;

export class BubbleMapChart extends Chart {
constructor(item, config) {
super(item, patchController(config, BubbleMapController, GeoFeature, [SizeScale, ProjectionScale]));
constructor(item: ChartItem, config: Omit<IBubbleMapControllerConfiguration<T, L>, 'type'>) {
super(item, patchController('bubbleMap', config, BubbleMapController, GeoFeature, [SizeScale, ProjectionScale]));
}
}
BubbleMapChart.id = BubbleMapController.id;
Loading

0 comments on commit 1591bc6

Please sign in to comment.