Skip to content
This repository was archived by the owner on Aug 21, 2021. It is now read-only.

Commit d055ba6

Browse files
author
Kevin Murek
committed
Add compiled files
1 parent e51fa9a commit d055ba6

File tree

13 files changed

+589
-4
lines changed

13 files changed

+589
-4
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ logs/
2121
build/
2222
coverage/
2323
dist/
24-
esm/
25-
lib/
24+
#esm/ # Exclude the compiled files because I don't have time to find a better way right now
25+
#lib/
2626
public/
2727
node_modules/
2828
tmp/
2929

3030
# Custom
31-
*.map
32-
*.min.js
31+
#*.map # Exclude the compiled files because I don't have time to find a better way right now
32+
#*.min.js
3333
test-changelog.md
3434

3535
# Configs (provided by Nimbus)
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import _pt from "prop-types";
2+
3+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4+
5+
import _ from 'lodash';
6+
import React from 'react';
7+
import { CategoricalColorNamespace } from '@superset-ui/color';
8+
import { getNumberFormatter } from '@superset-ui/number-format';
9+
import { AxisLeft, AxisBottom } from '@vx/axis';
10+
import { Grid } from '@vx/grid';
11+
import { Group } from '@vx/group';
12+
import { Bar } from '@vx/shape';
13+
import { scaleBand, scaleLinear } from '@vx/scale';
14+
import { Text } from '@vx/text';
15+
const {
16+
getScale
17+
} = CategoricalColorNamespace;
18+
export default class DiscreteBarChart extends React.PureComponent {
19+
getColor(value, colorScheme) {
20+
const colorFn = getScale(colorScheme);
21+
return colorFn(value.trim());
22+
}
23+
24+
render() {
25+
const {
26+
height,
27+
width,
28+
colorScheme,
29+
showBarValue,
30+
orderBars,
31+
xTicksLayout,
32+
yAxisFormat
33+
} = this.props;
34+
let {
35+
data
36+
} = this.props; // Layout for x ticks
37+
38+
let tickAngle = 0;
39+
let tickMargin = 0;
40+
let tickAnchor = 'middle';
41+
42+
if (xTicksLayout === 'auto' || xTicksLayout === '45°') {
43+
tickAngle = 45;
44+
tickMargin = 100;
45+
tickAnchor = 'start';
46+
}
47+
48+
const margin = {
49+
top: 30,
50+
bottom: 30 + tickMargin,
51+
left: 75,
52+
right: 20
53+
};
54+
const formatter = getNumberFormatter(yAxisFormat);
55+
56+
if (orderBars) {
57+
data = _.orderBy(data, ['key', 'asc']);
58+
} // Then we'll create some bounds
59+
60+
61+
const xMax = width - margin.left - margin.right;
62+
const yMax = height - margin.top - margin.bottom; // We'll make some helpers to get at the data we want
63+
64+
const x = d => d.key;
65+
66+
const y = d => d.value; // And then scale the graph by our data
67+
68+
69+
const xScale = scaleBand({
70+
rangeRound: [0, xMax],
71+
domain: data.map(x),
72+
padding: 0.1
73+
});
74+
const yScale = scaleLinear({
75+
rangeRound: [yMax, 0],
76+
domain: [0, Math.max(...data.map(y))]
77+
}); // Compose together the scale and accessor functions to get point functions
78+
79+
const compose = (scale, accessor) => data => scale(accessor(data));
80+
81+
const xPoint = compose(xScale, x);
82+
const yPoint = compose(yScale, y);
83+
return /*#__PURE__*/React.createElement("svg", {
84+
width: width,
85+
height: height
86+
}, /*#__PURE__*/React.createElement(Group, {
87+
top: margin.top,
88+
left: margin.left
89+
}, /*#__PURE__*/React.createElement(Grid, {
90+
xScale: xScale // @ts-ignore
91+
,
92+
yScale: yScale,
93+
width: xMax,
94+
height: yMax,
95+
numTicksRows: 5
96+
}), /*#__PURE__*/React.createElement(AxisLeft, {
97+
scale: yScale,
98+
top: 0,
99+
left: 0,
100+
hideTicks: true,
101+
numTicks: 5 // @ts-ignore
102+
,
103+
tickFormat: formatter,
104+
tickLabelProps: (tickValue, index) => {
105+
return {
106+
fontFamily: 'Arial,sans-serif',
107+
fontSize: 12,
108+
textAnchor: 'end'
109+
};
110+
}
111+
}), /*#__PURE__*/React.createElement(AxisBottom, {
112+
scale: xScale,
113+
top: yMax,
114+
stroke: '#1b1a1e',
115+
hideAxisLine: true,
116+
hideTicks: true,
117+
tickLabelProps: (tickValue, index) => {
118+
return {
119+
fontFamily: 'Arial,sans-serif',
120+
fontSize: 12,
121+
textAnchor: tickAnchor,
122+
angle: tickAngle
123+
};
124+
}
125+
}), /*#__PURE__*/React.createElement(Group, null, data.map((d, i) => {
126+
const barHeight = yMax - yPoint(d);
127+
return /*#__PURE__*/React.createElement("g", {
128+
key: d.key
129+
}, /*#__PURE__*/React.createElement(Bar, {
130+
x: xPoint(d),
131+
y: yMax - barHeight,
132+
height: barHeight,
133+
width: xScale.bandwidth() // fill={color(xPoint(d))}
134+
,
135+
fill: this.getColor(x(d), colorScheme)
136+
}), showBarValue && /*#__PURE__*/React.createElement(Text, {
137+
x: xPoint(d) + xScale.bandwidth() / 2,
138+
y: yMax - barHeight - 10,
139+
textAnchor: "middle",
140+
style: {
141+
fontFamily: 'Arial,sans-serif',
142+
fontSize: 12,
143+
textAnchor: 'middle'
144+
}
145+
}, formatter(d.value)));
146+
}))));
147+
}
148+
149+
}
150+
151+
_defineProperty(DiscreteBarChart, "propTypes", {
152+
height: _pt.number.isRequired,
153+
width: _pt.number.isRequired,
154+
colorScheme: _pt.string.isRequired,
155+
data: _pt.arrayOf(_pt.shape({
156+
key: _pt.string.isRequired,
157+
value: _pt.number.isRequired
158+
})).isRequired,
159+
showBarValue: _pt.bool.isRequired,
160+
orderBars: _pt.bool.isRequired,
161+
showLegend: _pt.bool.isRequired,
162+
xTicksLayout: _pt.string.isRequired,
163+
yAxisFormat: _pt.string.isRequired
164+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { t } from '@superset-ui/translation';
2+
import { ChartMetadata, ChartPlugin } from '@superset-ui/chart';
3+
import transformProps from './transformProps';
4+
5+
function nonEmpty(v) {
6+
if (v === null || v === undefined || v === '' || Array.isArray(v) && v.length === 0) {
7+
return t('cannot be empty');
8+
}
9+
10+
return false;
11+
}
12+
13+
const metadata = new ChartMetadata({
14+
description: '',
15+
name: t('Discrete bar chart'),
16+
thumbnail: ''
17+
});
18+
const controlPanel = {
19+
controlPanelSections: [{
20+
label: t('Query'),
21+
expanded: true,
22+
controlSetRows: [['metric'], ['adhoc_filters'], ['groupby'], ['row_limit']]
23+
}, {
24+
label: t('Chart Options'),
25+
expanded: true,
26+
controlSetRows: [['color_scheme', 'label_colors'], ['show_bar_value'], ['order_bars'], ['y_axis_format']]
27+
}, {
28+
label: t('X Axis'),
29+
expanded: true,
30+
controlSetRows: [['x_ticks_layout']]
31+
}],
32+
controlOverrides: {
33+
groupby: {
34+
label: t('Series'),
35+
validators: [nonEmpty]
36+
}
37+
}
38+
};
39+
export default class DiscreteBarChartPlugin extends ChartPlugin {
40+
constructor() {
41+
super({
42+
controlPanel,
43+
loadChart: () => import('./DiscreteBarChart'),
44+
metadata,
45+
transformProps
46+
});
47+
}
48+
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export default function transformProps(chartProps) {
2+
const {
3+
width,
4+
height,
5+
formData,
6+
queryData
7+
} = chartProps;
8+
const {
9+
bottomMargin,
10+
colorPicker,
11+
colorScheme,
12+
leftMargin,
13+
showBarValue,
14+
orderBars,
15+
showLegend,
16+
xTicksLayout,
17+
yAxisFormat
18+
} = formData;
19+
const {
20+
data
21+
} = queryData;
22+
return {
23+
width,
24+
height,
25+
data,
26+
baseColor: colorPicker,
27+
bottomMargin,
28+
colorScheme,
29+
leftMargin,
30+
showBarValue,
31+
orderBars,
32+
showLegend,
33+
xTicksLayout,
34+
yAxisFormat
35+
};
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
export declare type DiscreteBarDataPoint = {
3+
key: string;
4+
value: number;
5+
};
6+
export declare type DiscreteBarChartProps = {
7+
height: number;
8+
width: number;
9+
colorScheme: string;
10+
data: DiscreteBarDataPoint[];
11+
showBarValue: boolean;
12+
orderBars: boolean;
13+
showLegend: boolean;
14+
xTicksLayout: string;
15+
yAxisFormat: string;
16+
};
17+
export declare type AxisTickAnchor = 'start' | 'middle' | 'end' | 'inherit';
18+
export default class DiscreteBarChart extends React.PureComponent<DiscreteBarChartProps> {
19+
getColor(value: any, colorScheme: string): any;
20+
render(): JSX.Element;
21+
}
22+
//# sourceMappingURL=DiscreteBarChart.d.ts.map

packages/superset-ui-plugin-chart-discrete-bar/lib/DiscreteBarChart.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)