Skip to content

Commit

Permalink
fix(axis): better support small time scales
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdickerson committed Dec 12, 2019
1 parent e115968 commit 8cfa595
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 128 deletions.
239 changes: 120 additions & 119 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,121 +1,122 @@
{
"name": "@carbon/charts",
"version": "0.20.1",
"description": "Carbon charting components",
"main": "./bundle.js",
"module": "./index.js",
"scripts": {
"demo:server": "webpack-dev-server --config webpack.config.js --watch",
"demo:build": "webpack --config webpack.config.js && yarn run docs:build",
"docs:build": "typedoc --out ./demo/bundle/documentation ./src/index.ts",
"build": "bash build.sh",
"postinstall": "bash build-vendor.sh",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "karma start --single-run",
"test:watch": "karma start --no-single-run",
"clean": "rm -rf dist demo/bundle"
},
"repository": {
"type": "git",
"url": "git@github.com:IBM/carbon-charts.git"
},
"keywords": [
"carbon",
"charts",
"dataviz",
"data-visualization",
"visualizations",
"d3",
"svg",
"component",
"components",
"css",
"html",
"ibm",
"typescript",
"javascript",
"js",
"library",
"pattern",
"patterns",
"sass",
"scss"
],
"author": "IBM",
"license": "Apache-2.0",
"dependencies": {
"@carbon/utils-position": "1.1.1",
"babel-polyfill": "6.26.0",
"carbon-components": "10.5.0",
"lodash-es": "4.17.15",
"resize-observer-polyfill": "1.5.0"
},
"peerDependencies": {
"d3": ">=5.0.0 <=5.9.2"
},
"devDependencies": {
"@carbon/colors": "10.4.0",
"@carbon/import-once": "10.3.0",
"@carbon/layout": "10.5.0",
"@carbon/motion": "10.4.0",
"@carbon/themes": "10.4.0",
"@carbon/type": "10.5.1",
"@types/d3": "4.11.0",
"@types/jasmine": "2.8.7",
"@types/karma": "3.0.2",
"@types/node": "12.11.7",
"babel-polyfill": "6.26.0",
"copy-webpack-plugin": "4.5.2",
"css-loader": "0.28.7",
"d3": "5.9.2",
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.5",
"html-loader": "0.5.1",
"html-webpack-exclude-assets-plugin": "0.0.7",
"html-webpack-plugin": "3.2.0",
"jasmine-core": "3.4.0",
"karma": "4.0.1",
"karma-chrome-launcher": "2.2.0",
"karma-firefox-launcher": "1.1.0",
"karma-jasmine": "2.0.1",
"karma-safari-launcher": "1.0.0",
"karma-webpack": "3.0.5",
"lerna": "3.13.4",
"mini-css-extract-plugin": "0.4.4",
"node-sass": "4.10.0",
"raw-loader": "0.5.1",
"rollup": "1.21.4",
"rollup-plugin-commonjs": "10.1.0",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-terser": "5.1.2",
"rollup-plugin-typescript2": "0.24.2",
"sass-loader": "8.0.0",
"style-loader": "0.19.0",
"ts-loader": "5.1.0",
"tslint": "5.8.0",
"tslint-loader": "3.5.3",
"typedoc": "0.11.1",
"typescript": "3.2.2",
"url-loader": "0.6.2",
"webpack": "4.41.0",
"webpack-cli": "3.3.9",
"webpack-dev-server": "3.7.0"
},
"publishConfig": {
"access": "public"
},
"maintainers": [
{
"name": "Eliad Moosavi",
"email": "iliadm@ca.ibm.com",
"url": "https://github.com/theiliad"
}
],
"contributors": [
{
"name": "Eliad Moosavi",
"email": "iliadm@ca.ibm.com",
"url": "https://github.com/theiliad"
}
]
"name": "@carbon/charts",
"version": "0.20.1",
"description": "Carbon charting components",
"main": "./bundle.js",
"module": "./index.js",
"scripts": {
"demo:server": "webpack-dev-server --config webpack.config.js --watch",
"demo:build": "webpack --config webpack.config.js && yarn run docs:build",
"docs:build": "typedoc --out ./demo/bundle/documentation ./src/index.ts",
"build": "bash build.sh",
"postinstall": "bash build-vendor.sh",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "karma start --single-run",
"test:watch": "karma start --no-single-run",
"clean": "rm -rf dist demo/bundle"
},
"repository": {
"type": "git",
"url": "git@github.com:IBM/carbon-charts.git"
},
"keywords": [
"carbon",
"charts",
"dataviz",
"data-visualization",
"visualizations",
"d3",
"svg",
"component",
"components",
"css",
"html",
"ibm",
"typescript",
"javascript",
"js",
"library",
"pattern",
"patterns",
"sass",
"scss"
],
"author": "IBM",
"license": "Apache-2.0",
"dependencies": {
"@carbon/utils-position": "1.1.1",
"babel-polyfill": "6.26.0",
"carbon-components": "10.5.0",
"date-fns": "2.8.1",
"lodash-es": "4.17.15",
"resize-observer-polyfill": "1.5.0"
},
"peerDependencies": {
"d3": ">=5.0.0 <=5.9.2"
},
"devDependencies": {
"@carbon/colors": "10.4.0",
"@carbon/import-once": "10.3.0",
"@carbon/layout": "10.5.0",
"@carbon/motion": "10.4.0",
"@carbon/themes": "10.4.0",
"@carbon/type": "10.5.1",
"@types/d3": "4.11.0",
"@types/jasmine": "2.8.7",
"@types/karma": "3.0.2",
"@types/node": "12.11.7",
"babel-polyfill": "6.26.0",
"copy-webpack-plugin": "4.5.2",
"css-loader": "0.28.7",
"d3": "5.9.2",
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.5",
"html-loader": "0.5.1",
"html-webpack-exclude-assets-plugin": "0.0.7",
"html-webpack-plugin": "3.2.0",
"jasmine-core": "3.4.0",
"karma": "4.0.1",
"karma-chrome-launcher": "2.2.0",
"karma-firefox-launcher": "1.1.0",
"karma-jasmine": "2.0.1",
"karma-safari-launcher": "1.0.0",
"karma-webpack": "3.0.5",
"lerna": "3.13.4",
"mini-css-extract-plugin": "0.4.4",
"node-sass": "4.10.0",
"raw-loader": "0.5.1",
"rollup": "1.21.4",
"rollup-plugin-commonjs": "10.1.0",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-terser": "5.1.2",
"rollup-plugin-typescript2": "0.24.2",
"sass-loader": "8.0.0",
"style-loader": "0.19.0",
"ts-loader": "5.1.0",
"tslint": "5.8.0",
"tslint-loader": "3.5.3",
"typedoc": "0.11.1",
"typescript": "3.2.2",
"url-loader": "0.6.2",
"webpack": "4.41.0",
"webpack-cli": "3.3.9",
"webpack-dev-server": "3.7.0"
},
"publishConfig": {
"access": "public"
},
"maintainers": [
{
"name": "Eliad Moosavi",
"email": "iliadm@ca.ibm.com",
"url": "https://github.com/theiliad"
}
],
"contributors": [
{
"name": "Eliad Moosavi",
"email": "iliadm@ca.ibm.com",
"url": "https://github.com/theiliad"
}
]
}
44 changes: 35 additions & 9 deletions packages/core/src/components/axes/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import { scaleBand, scaleLinear, scaleTime, scaleLog, scaleOrdinal } from "d3-sc
import { axisBottom, axisLeft, axisRight, axisTop } from "d3-axis";
import { min, extent } from "d3-array";
import { timeFormatDefaultLocale } from "d3-time-format";
import {
differenceInYears,
addYears,
subYears,
differenceInMonths,
addMonths,
subMonths,
differenceInDays,
addDays,
subDays,
differenceInHours,
addHours,
subHours,
differenceInMinutes,
addMinutes,
subMinutes
} from "date-fns";

export class Axis extends Component {
type = "axes";
Expand Down Expand Up @@ -131,16 +148,25 @@ export class Axis extends Component {

if (axisOptions.scaleType === ScaleTypes.TIME) {
if (Tools.getProperty(options, "timeScale", "addSpaceOnEdges")) {
// TODO - Need to account for non-day incrementals as well
const [startDate, endDate] = domain;
startDate.setDate(startDate.getDate() - 1);
endDate.setDate(endDate.getDate() + 1);
}
const startDate = new Date(domain[0]);
const endDate = new Date(domain[1]);
if (differenceInYears(endDate, startDate) > 3) {
return [subYears(startDate, 1), addYears(endDate, 1)];
}
if (differenceInMonths(endDate, startDate) > 3) {
return [subMonths(startDate, 1), addMonths(endDate, 1)];
}
if (differenceInDays(endDate, startDate) > 3) {
return [subDays(startDate, 1), addDays(endDate, 1)];
} else if (differenceInHours(endDate, startDate) > 3) {
return [subHours(startDate, 1), addHours(endDate, 1)];
} else if (differenceInMinutes(endDate, startDate) > 3) {
return [subMinutes(startDate, 1), addMinutes(endDate, 1)];
}
// Other
return [startDate, endDate];
}

return [
new Date(domain[0]),
new Date(domain[1])
];
}

// TODO - Work with design to improve logic
Expand Down
Binary file added yarn-offline-mirror-cache/date-fns-2.8.1.tgz
Binary file not shown.
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8013,6 +8013,11 @@ date-fns@^1.27.2:
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c"
integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==

date-fns@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.8.1.tgz#2109362ccb6c87c3ca011e9e31f702bc09e4123b"
integrity sha512-EL/C8IHvYRwAHYgFRse4MGAPSqlJVlOrhVYZ75iQBKrnv+ZedmYsgwH3t+BCDuZDXpoo07+q9j4qgSSOa7irJg==

date-format@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/date-format/-/date-format-1.2.0.tgz#615e828e233dd1ab9bb9ae0950e0ceccfa6ecad8"
Expand Down

0 comments on commit 8cfa595

Please sign in to comment.