Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the format of long numbers #573

Merged
merged 6 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix the format of long numbers by using SI format
  • Loading branch information
matt-hooper committed May 8, 2019
commit 9554a3f7df8ba4fab8e5619df6ec7008d867faa2
3 changes: 2 additions & 1 deletion packages/perspective-viewer-d3fc/src/js/axis/axisFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export const axisFactory = settings => {
},
size: component.size,
decorate: component.decorate,
label: settings[settingName].map(v => v.name).join(", ")
label: settings[settingName].map(v => v.name).join(", "),
type: useType
};
};

Expand Down
19 changes: 19 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/axis/valueFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
import * as d3 from "d3";
import {AXIS_TYPES} from "./axisType";

const SI_MIN = 10000000;

export default axisType => {
if (axisType == AXIS_TYPES.linear) {
return d => (Math.abs(d) >= SI_MIN ? d3.format(".3s")(d) : Number.isInteger(d) ? d3.format(",.0f")(d) : d3.format(",.2f")(d));
}
return null;
};
2 changes: 2 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {splitAndBaseData} from "../data/splitAndBaseData";
import {colorLegend} from "../legend/legend";
import {filterData} from "../legend/filter";
import withGridLines from "../gridlines/gridlines";
import valueformatter from "../axis/valueFormatter";

import {hardLimitZeroPadding} from "../d3fc/padding/hardLimitZero";
import zoomableChart from "../zoom/zoomableChart";
Expand Down Expand Up @@ -46,6 +47,7 @@ function areaChart(container, settings) {
const chart = chartSvgFactory(xAxis, yAxis).plotArea(withGridLines(series).orient("vertical"));

chart.yNice && chart.yNice();
chart.yTickFormat && chart.yTickFormat(valueformatter(yAxis.type));

const zoomChart = zoomableChart()
.chart(chart)
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import withGridLines from "../gridlines/gridlines";

import {hardLimitZeroPadding} from "../d3fc/padding/hardLimitZero";
import zoomableChart from "../zoom/zoomableChart";
import valueformatter from "../axis/valueFormatter";

function barChart(container, settings) {
const data = groupAndStackData(settings, filterData(settings));
Expand Down Expand Up @@ -54,6 +55,7 @@ function barChart(container, settings) {
bars.align("left");
}
chart.xNice && chart.xNice();
chart.xTickFormat && chart.xTickFormat(valueformatter);

const zoomChart = zoomableChart()
.chart(chart)
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {filterData} from "../legend/filter";
import withGridLines from "../gridlines/gridlines";
import {hardLimitZeroPadding} from "../d3fc/padding/hardLimitZero";
import zoomableChart from "../zoom/zoomableChart";
import valueformatter from "../axis/valueFormatter";

function columnChart(container, settings) {
const data = groupAndStackData(settings, filterData(settings));
Expand Down Expand Up @@ -53,6 +54,7 @@ function columnChart(container, settings) {
bars.align("left");
}
chart.yNice && chart.yNice();
chart.yTickFormat && chart.yTickFormat(valueformatter(yAxis.type));

const zoomChart = zoomableChart()
.chart(chart)
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import withGridLines from "../gridlines/gridlines";
import {hardLimitZeroPadding} from "../d3fc/padding/hardLimitZero";
import zoomableChart from "../zoom/zoomableChart";
import nearbyTip from "../tooltip/nearbyTip";
import valueformatter from "../axis/valueFormatter";

function lineChart(container, settings) {
const data = splitData(settings, filterData(settings));
Expand Down Expand Up @@ -49,6 +50,7 @@ function lineChart(container, settings) {
const chart = chartSvgFactory(xAxis, yAxis).plotArea(withGridLines(series).orient("vertical"));

chart.yNice && chart.yNice();
chart.yTickFormat && chart.yTickFormat(valueformatter);

const zoomChart = zoomableChart()
.chart(chart)
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/ohlcCandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import nearbyTip from "../tooltip/nearbyTip";
import {ohlcCandleSeries} from "../series/ohlcCandleSeries";
import {colorScale, setOpacity} from "../series/seriesColors";
import {colorLegend} from "../legend/legend";
import valueformatter from "../axis/valueFormatter";

function ohlcCandle(seriesCanvas) {
return function(container, settings) {
Expand Down Expand Up @@ -71,6 +72,7 @@ function ohlcCandle(seriesCanvas) {
);

chart.yNice && chart.yNice();
chart.yTickFormat && chart.yTickFormat(valueformatter(yAxis.type));

const zoomChart = zoomableChart()
.chart(chart)
Expand Down
4 changes: 4 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/xy-scatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import withGridLines from "../gridlines/gridlines";
import {hardLimitZeroPadding} from "../d3fc/padding/hardLimitZero";
import zoomableChart from "../zoom/zoomableChart";
import nearbyTip from "../tooltip/nearbyTip";
import valueformatter from "../axis/valueFormatter";

function xyScatter(container, settings) {
const data = pointData(settings, filterDataByGroup(settings));
Expand Down Expand Up @@ -69,6 +70,9 @@ function xyScatter(container, settings) {
chart.xNice && chart.xNice();
chart.yNice && chart.yNice();

chart.yTickFormat && chart.yTickFormat(valueformatter(yAxis.type));
chart.xTickFormat && chart.xTickFormat(valueformatter(xAxis.type));

const zoomChart = zoomableChart()
.chart(chart)
.settings(settings)
Expand Down
2 changes: 2 additions & 0 deletions packages/perspective-viewer-d3fc/src/js/charts/y-scatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import withGridLines from "../gridlines/gridlines";
import {hardLimitZeroPadding} from "../d3fc/padding/hardLimitZero";
import zoomableChart from "../zoom/zoomableChart";
import nearbyTip from "../tooltip/nearbyTip";
import valueformatter from "../axis/valueFormatter";

function yScatter(container, settings) {
const data = groupData(settings, filterData(settings));
Expand Down Expand Up @@ -52,6 +53,7 @@ function yScatter(container, settings) {
const chart = chartSvgFactory(xAxis, yAxis).plotArea(withGridLines(series).orient("vertical"));

chart.yNice && chart.yNice();
chart.yTickFormat && chart.yTickFormat(valueformatter(yAxis.type));

const zoomChart = zoomableChart()
.chart(chart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import * as d3 from "d3";
import * as fc from "d3fc";
import {getOrCreateElement} from "../utils/utils";
import valueformatter from "../axis/valueFormatter";
import {AXIS_TYPES} from "../axis/axisType";

export function colorRangeLegend() {
let scale = null;

const formatFunc = d => (Number.isInteger(d) ? d3.format(",.0f")(d) : d3.format(",.2f")(d));

function legend(container) {
const legendSelection = getOrCreateElement(container, "div.legend-container", () =>
container
Expand Down Expand Up @@ -63,7 +63,7 @@ export function colorRangeLegend() {
.axisRight(yScale)
.tickValues(tickValues)
.tickSizeOuter(0)
.tickFormat(d => formatFunc(d));
.tickFormat(d => valueformatter(AXIS_TYPES.linear)(d));

const legendSvg = getOrCreateElement(legendSelection, "svg", () => legendSelection.append("svg"))
.style("width", width)
Expand Down