Skip to content

Commit

Permalink
Custom tick values (#713)
Browse files Browse the repository at this point in the history
* chore(angular): add angular 9 dependency versions to peerDependencies

* improvement(core): add functionality for custom tick values being passed in

fix #540

* add time series demo

* fix label scale

* update demo

* review changes

Co-authored-by: Eliad Moosavi <[email protected]>
Co-authored-by: Fei Z <[email protected]>
  • Loading branch information
3 people authored Jul 21, 2020
1 parent c1e36c0 commit 8827f55
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/core/demo/data/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ export const simpleBarOptions = {
}
};

// Simple bar with custom tick values
export const simpleBarDataCustomTicks = [
{ group: "Group 1", value: 0.5 },
{ group: "Group 2", value: 2 }
];

export const simpleBarOptionsCustomTicks = {
title: "Simple bar (custom ticks)",
axes: {
left: {
mapsTo: "value",
ticks: {
values: [0, 1.2, 1.3, 2]
}
},
bottom: {
mapsTo: "group",
scaleType: "labels"
}
}
};

// Simple bar with long labels
export const simpleBarLongLabelData = [
{
Expand Down Expand Up @@ -322,6 +344,26 @@ export const stackedBarTimeSeriesOptions = {
}
};

// demo with custom ticks
export const stackedBarTimeSeriesDataCustomTicks = stackedBarTimeSeriesData;

export const stackedBarTimeSeriesOptionsCustomTicks = {
title: "Stacked bar (time series - custom ticks)",
axes: {
left: {
mapsTo: "value",
stacked: true
},
bottom: {
mapsTo: "date",
scaleType: "time",
ticks: {
values: [new Date(2019, 0, 17)]
}
}
}
};

// Stacked horizontal bar (time series)
export const stackedHorizontalBarTimeSeriesOptions = {
title: "Stacked horizontal bar (time series)",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/demo/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ let allDemoGroups = [
data: barDemos.simpleBarData,
chartType: chartTypes.SimpleBarChart
},
{
options: barDemos.simpleBarOptionsCustomTicks,
data: barDemos.simpleBarDataCustomTicks,
chartType: chartTypes.SimpleBarChart,
isDemoExample: false
},
{
options: barDemos.simpleBarLongLabelOptions,
data: barDemos.simpleBarLongLabelData,
Expand Down Expand Up @@ -212,6 +218,12 @@ let allDemoGroups = [
data: barDemos.stackedBarTimeSeriesData,
chartType: chartTypes.StackedBarChart
},
{
options: barDemos.stackedBarTimeSeriesOptionsCustomTicks,
data: barDemos.stackedBarTimeSeriesDataCustomTicks,
chartType: chartTypes.StackedBarChart,
isDemoExample: false
},
{
options: barDemos.stackedBarEmptyStateOptions,
data: barDemos.stackedBarEmptyStateData,
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/components/axes/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export class Axis extends Component {
"number"
);

// user can provide custom ticks to be displayed
// ticks need to be in the domain of the axis data
const userProvidedTickValues = Tools.getProperty(
axisOptions,
"ticks",
"values"
);

// get user provided custom values for truncation
const truncationType = Tools.getProperty(
axisOptions,
Expand Down Expand Up @@ -256,6 +264,12 @@ export class Axis extends Component {
// Set ticks formatter
axis.tickFormat(formatter);

// prioritize using a custom array of values rather than number of ticks
// if both are provided. custom tick values need to be within the domain
if (userProvidedTickValues) {
axis.tickValues(userProvidedTickValues);
}

// Position and transition the axis
switch (axisPosition) {
case AxisPositions.LEFT:
Expand Down Expand Up @@ -457,7 +471,8 @@ export class Axis extends Component {
// only applies to discrete type
if (
truncationType !== TruncationTypes.NONE &&
axisScaleType === ScaleTypes.LABELS
axisScaleType === ScaleTypes.LABELS &&
!userProvidedTickValues
) {
const dataGroups = this.model.getDataValuesGroupedByKeys();
if (dataGroups.length > 0) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/interfaces/axis-scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export interface AxisOptions {
* function to format the ticks
*/
formatter?: Function;
/**
* optional custom array of tick values that is within the domain of data
*/
values?: any[]
};
truncation?: TruncationOptions;
}
Expand Down

0 comments on commit 8827f55

Please sign in to comment.