Skip to content

Commit

Permalink
feat: allow user to set zoom bar data
Browse files Browse the repository at this point in the history
  • Loading branch information
hlyang397 committed Jul 21, 2020
1 parent abfd3e1 commit e1d4636
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/core/demo/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ let allDemoGroups = [
chartType: chartTypes.StackedBarChart,
isDemoExample: false
},
{
options: zoomBarDemos.definedZoomBarStackedBarTimeSeriesOptions,
data: zoomBarDemos.definedZoomBarStackedBarTimeSeriesData,
chartType: chartTypes.StackedBarChart,
isDemoExample: false
},
{
options: zoomBarDemos.zoomBarBubbleTimeSeriesOptions,
data: zoomBarDemos.zoomBarBubbleTimeSeriesData,
Expand Down
31 changes: 28 additions & 3 deletions packages/core/demo/data/zoom-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,28 @@ const initialZoomDomain = [
new Date(2020, 11, 11, 0, 0, 25)
];

const definedZoomBarData = [
{ date: new Date(2019, 0, 1), value: 10000 },
{ date: new Date(2019, 0, 2), value: 10 },
{ date: new Date(2019, 0, 3), value: 75000 },
{ date: new Date(2019, 0, 5), value: 65000 },
{ date: new Date(2019, 0, 6), value: 57312 },
{ date: new Date(2019, 0, 8), value: 10000 },
{ date: new Date(2019, 0, 13), value: 49213 },
{ date: new Date(2019, 0, 15), value: 70323 },
{ date: new Date(2019, 0, 17), value: 51213 },
{ date: new Date(2019, 0, 19), value: 21300 }
];

// utility function to update title and enable zoomBar option
const addZoomBarToOptions = (options) => {
options["title"] = options["title"] + " - Zoom bar enabled";
options["zoomBar"] = { enabled: true };
const addZoomBarToOptions = (options, includeDefinedZoomBarData = false) => {
if (includeDefinedZoomBarData) {
options["title"] = options["title"] + " - Defined zoom bar enabled";
options["zoomBar"] = { enabled: true, data: definedZoomBarData };
} else {
options["title"] = options["title"] + " - Zoom bar enabled";
options["zoomBar"] = { enabled: true };
}
return options;
};

Expand All @@ -35,6 +53,13 @@ export const zoomBarStackedBarTimeSeriesOptions = addZoomBarToOptions(
Object.assign({}, barChart.stackedBarTimeSeriesOptions)
);

export const definedZoomBarStackedBarTimeSeriesData =
barChart.stackedBarTimeSeriesData;
export const definedZoomBarStackedBarTimeSeriesOptions = addZoomBarToOptions(
Object.assign({}, barChart.stackedBarTimeSeriesOptions),
true
);

export const zoomBarBubbleTimeSeriesData = bubbleChart.bubbleTimeSeriesData;
export const zoomBarBubbleTimeSeriesOptions = addZoomBarToOptions(
Object.assign({}, bubbleChart.bubbleTimeSeriesOptions)
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/interfaces/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ export interface ZoomBarOptions {
* an two element array which represents the initial zoom domain
*/
initialZoomDomain?: Object[];
/**
* options related to zoom bar data
*/
data?: Object[];
}
19 changes: 16 additions & 3 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,23 @@ export class ChartModel {
const domainIdentifier = cartesianScales.getDomainIdentifier();
const rangeIdentifier = cartesianScales.getRangeIdentifier();

const displayData = this.getDisplayData();
let zoomBarData;
// check if pre-defined zoom bar data exists
const definedZoomBarData = Tools.getProperty(
this.getOptions(),
"zoomBar",
"data"
);
// if user already defines zoom bar data, use it
if (definedZoomBarData) {
zoomBarData = definedZoomBarData;
} else {
// use displayData if not defined
zoomBarData = this.getDisplayData();
}
// get all dates (Number) in displayData
let allDates = [];
displayData.forEach((data) => {
zoomBarData.forEach((data) => {
allDates = allDates.concat(Number(data[domainIdentifier]));
});
allDates = Tools.removeArrayDuplicates(allDates).sort();
Expand All @@ -59,7 +72,7 @@ export class ChartModel {
let sum = 0;
const datum = {};

displayData.forEach((data) => {
zoomBarData.forEach((data) => {
if (Number(data[domainIdentifier]) === date) {
sum += data[rangeIdentifier];
}
Expand Down

0 comments on commit e1d4636

Please sign in to comment.