amazing-react-charts 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,10 +9,14 @@ Object.defineProperty(exports, "__esModule", {
9
9
  });
10
10
  exports.default = void 0;
11
11
 
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
12
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
13
13
 
14
14
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
15
15
 
16
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
+
18
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
19
+
16
20
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
17
21
 
18
22
  var _react = _interopRequireWildcard(require("react"));
@@ -59,6 +63,11 @@ var HorizontalBarChart = function HorizontalBarChart(props) {
59
63
  title = _useState2[0],
60
64
  setTitle = _useState2[1];
61
65
 
66
+ var _useState3 = (0, _react.useState)([]),
67
+ _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
68
+ richData = _useState4[0],
69
+ setRichDate = _useState4[1];
70
+
62
71
  (0, _react.useEffect)(function () {
63
72
  if (toolboxTooltip && toolboxTooltip.saveAsImageWithTitle) {
64
73
  setTitle(false);
@@ -66,6 +75,60 @@ var HorizontalBarChart = function HorizontalBarChart(props) {
66
75
  setTitle(true);
67
76
  }
68
77
  }, [toolboxTooltip]);
78
+ (0, _react.useEffect)(function () {
79
+ data.map( /*#__PURE__*/function () {
80
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(item) {
81
+ var rich;
82
+ return _regenerator.default.wrap(function _callee$(_context) {
83
+ while (1) {
84
+ switch (_context.prev = _context.next) {
85
+ case 0:
86
+ if (item.image) {
87
+ _context.next = 2;
88
+ break;
89
+ }
90
+
91
+ return _context.abrupt("return", {});
92
+
93
+ case 2:
94
+ _context.t0 = _defineProperty2.default;
95
+ _context.t1 = {};
96
+ _context.t2 = (0, _auxiliarFunctions.changeSpaceForUnderline)(item.label);
97
+ _context.next = 7;
98
+ return (0, _auxiliarFunctions.convertImageToBase64FromUrl)(item.image);
99
+
100
+ case 7:
101
+ _context.t3 = _context.sent;
102
+ _context.t4 = {
103
+ image: _context.t3
104
+ };
105
+ _context.t5 = {
106
+ height: 35,
107
+ backgroundColor: _context.t4
108
+ };
109
+ rich = (0, _context.t0)(_context.t1, _context.t2, _context.t5);
110
+
111
+ if (!richData.find(function (itemRich) {
112
+ return (0, _auxiliarFunctions.changeSpaceForUnderline)(item.label) in itemRich;
113
+ })) {
114
+ setRichDate(function (state) {
115
+ return [].concat((0, _toConsumableArray2.default)(state), [rich]);
116
+ });
117
+ }
118
+
119
+ case 12:
120
+ case "end":
121
+ return _context.stop();
122
+ }
123
+ }
124
+ }, _callee);
125
+ }));
126
+
127
+ return function (_x) {
128
+ return _ref.apply(this, arguments);
129
+ };
130
+ }());
131
+ }, [richData]);
69
132
 
70
133
  var handleShowTitle = function handleShowTitle(show) {
71
134
  setTitle(show);
@@ -104,9 +167,9 @@ var HorizontalBarChart = function HorizontalBarChart(props) {
104
167
  itemId: item.itemId
105
168
  };
106
169
  }));
107
- var yData = (0, _reverse.default)(data.map(function (item) {
170
+ var yData = data.map(function (item) {
108
171
  return item.label;
109
- }));
172
+ });
110
173
  var domain = {
111
174
  min: 0,
112
175
  max: Math.max.apply(Math, (0, _toConsumableArray2.default)(data.map(function (item) {
@@ -214,14 +277,20 @@ var HorizontalBarChart = function HorizontalBarChart(props) {
214
277
  yAxis: {
215
278
  data: yData,
216
279
  type: 'category',
280
+ inverse: true,
217
281
  axisLine: {
218
282
  show: showTickInfos || false
219
283
  },
220
284
  axisLabel: {
221
285
  formatter: function formatter(text) {
222
- return (0, _auxiliarFunctions.truncateLabel)(text, labelWordSize);
286
+ return data.find(function (item) {
287
+ return item.image;
288
+ }) ? (0, _auxiliarFunctions.formatLabelWithImage)(text) : (0, _auxiliarFunctions.truncateLabel)(text, labelWordSize);
223
289
  },
224
- fontWeight: boldTickLabel ? '400' : undefined
290
+ max: 10,
291
+ margin: 12,
292
+ fontWeight: boldTickLabel ? '400' : undefined,
293
+ rich: Object.assign.apply(Object, [{}].concat((0, _toConsumableArray2.default)(richData)))
225
294
  },
226
295
  axisTick: {
227
296
  show: showTickInfos || false,
@@ -62,6 +62,7 @@ export declare type EntryData = {
62
62
  result: number;
63
63
  itemId?: string;
64
64
  style?: LabelProps;
65
+ image?: string;
65
66
  };
66
67
  export declare type EntryWithStyleData = {
67
68
  value: number;
@@ -62,3 +62,6 @@ export declare const getSaveAsImageWithTitle: (title: string, setTitle: (show: b
62
62
  export declare const getWidthOpts: (width: WidthProps) => {
63
63
  width: WidthProps;
64
64
  };
65
+ export declare const convertImageToBase64FromUrl: (imgUrl: string) => Promise<unknown>;
66
+ export declare const changeSpaceForUnderline: (value: string) => string;
67
+ export declare const formatLabelWithImage: (text: string) => string;
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.getWidthOpts = exports.getSaveAsImageWithTitle = exports.thousandSeparator = exports.getInitialValues = exports.getDataView = exports.getSaveAsImage = exports.fixedDomain = exports.getDomain = exports.fixedTruncateLabel = exports.truncateLabel = exports.formatTooltipWithHours = exports.formatTooltip = exports.formatTime = exports.toDate = exports.mountMessage = exports.monuntTimeMessage = exports.generateAuxMessage = exports.getPercentage = exports.timeConvert = exports.takeDonutChartComplement = exports.takeLabelComplement = void 0;
8
+ exports.formatLabelWithImage = exports.changeSpaceForUnderline = exports.convertImageToBase64FromUrl = exports.getWidthOpts = exports.getSaveAsImageWithTitle = exports.thousandSeparator = exports.getInitialValues = exports.getDataView = exports.getSaveAsImage = exports.fixedDomain = exports.getDomain = exports.fixedTruncateLabel = exports.truncateLabel = exports.formatTooltipWithHours = exports.formatTooltip = exports.formatTime = exports.toDate = exports.mountMessage = exports.monuntTimeMessage = exports.generateAuxMessage = exports.getPercentage = exports.timeConvert = exports.takeDonutChartComplement = exports.takeLabelComplement = void 0;
9
9
 
10
10
  var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11
11
 
@@ -290,4 +290,42 @@ var getWidthOpts = function getWidthOpts(width) {
290
290
  };
291
291
  };
292
292
 
293
- exports.getWidthOpts = getWidthOpts;
293
+ exports.getWidthOpts = getWidthOpts;
294
+
295
+ var convertImageToBase64FromUrl = function convertImageToBase64FromUrl(imgUrl) {
296
+ return new Promise(function (resolve) {
297
+ var img = new Image();
298
+ img.src = imgUrl;
299
+ img.setAttribute('crossOrigin', 'anonymous');
300
+
301
+ img.onload = function () {
302
+ var canvas = document.createElement('canvas');
303
+ var ctx = canvas.getContext('2d');
304
+ canvas.width = 40;
305
+ canvas.height = 40;
306
+ ctx.save();
307
+ ctx.beginPath();
308
+ ctx.arc(40 / 2, 40 / 2, 40 / 2, 0, Math.PI * 2);
309
+ ctx.clip();
310
+ ctx.drawImage(img, 0, 0, 40, 40);
311
+ ctx.restore();
312
+ var dataUrl = ctx.canvas.toDataURL('image/png');
313
+ resolve(dataUrl);
314
+ };
315
+ });
316
+ };
317
+
318
+ exports.convertImageToBase64FromUrl = convertImageToBase64FromUrl;
319
+
320
+ var changeSpaceForUnderline = function changeSpaceForUnderline(value) {
321
+ return value.replace(' ', '_');
322
+ };
323
+
324
+ exports.changeSpaceForUnderline = changeSpaceForUnderline;
325
+
326
+ var formatLabelWithImage = function formatLabelWithImage(text) {
327
+ var textFormatted = '{value|' + text + '} {' + changeSpaceForUnderline(text) + '| }';
328
+ return textFormatted;
329
+ };
330
+
331
+ exports.formatLabelWithImage = formatLabelWithImage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amazing-react-charts",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "An amazing react charts package based in echarts",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -1,4 +1,4 @@
1
- import React, { useState, useEffect } from 'react'
1
+ import React, { useState, useEffect, useCallback } from 'react'
2
2
  import ReactEcharts from 'echarts-for-react'
3
3
  import {
4
4
  IDefaultChartProps,
@@ -16,16 +16,19 @@ import {
16
16
  truncateLabel,
17
17
  takeLabelComplement,
18
18
  getSaveAsImageWithTitle,
19
- getWidthOpts
19
+ getWidthOpts,
20
+ convertImageToBase64FromUrl,
21
+ changeSpaceForUnderline,
22
+ formatLabelWithImage,
20
23
  } from '../lib/auxiliarFunctions'
21
24
  import { reverse } from 'ramda'
22
25
  import { WIDTH_STYLE } from './DonutChart'
23
26
  import { TOOLBOX_DEFAULT_PROPS } from './AreaChart'
24
27
 
25
28
  interface IProps extends IDefaultChartProps {
26
- showTickInfos?: boolean
27
- xComplement?: string
28
- boldTickLabel?: boolean
29
+ showTickInfos?: boolean;
30
+ xComplement?: string;
31
+ boldTickLabel?: boolean;
29
32
  }
30
33
 
31
34
  const HorizontalBarChart = (props: IProps) => {
@@ -50,6 +53,7 @@ const HorizontalBarChart = (props: IProps) => {
50
53
  } = props
51
54
 
52
55
  const [title, setTitle] = useState(false)
56
+ const [richData, setRichDate] = useState([])
53
57
 
54
58
  useEffect(() => {
55
59
  if (toolboxTooltip && toolboxTooltip.saveAsImageWithTitle) {
@@ -59,18 +63,39 @@ const HorizontalBarChart = (props: IProps) => {
59
63
  }
60
64
  }, [toolboxTooltip])
61
65
 
66
+ useEffect(() => {
67
+ data.map(async (item: EntryData) => {
68
+ if(!item.image) return {}
69
+
70
+ const rich = {[changeSpaceForUnderline(item.label)]: {
71
+ height: 35,
72
+ backgroundColor: {
73
+ image: await convertImageToBase64FromUrl(item.image)
74
+ },
75
+ }}
76
+
77
+ if(!richData
78
+ .find(itemRich => changeSpaceForUnderline(item.label) in itemRich
79
+ )){
80
+ setRichDate(state => [...state, rich])
81
+ }
82
+
83
+ })
84
+
85
+ }, [richData])
86
+
62
87
  const handleShowTitle = (show: boolean) => {
63
88
  setTitle(show)
64
89
  }
65
90
 
66
91
  const xData: EntryWithStyleData[] = reverse(
67
92
  data.map((item: EntryData) => {
68
- const results = data.map(item => item.result)
93
+ const results = data.map((item) => item.result)
69
94
  const maxValue = Math.max(...results)
70
95
 
71
96
  const label: LabelProps = item.result <= (!showTickInfos ? 50 : 15) && {
72
97
  position: 'right',
73
- distance: 1
98
+ distance: 1,
74
99
  }
75
100
 
76
101
  if (maxValue !== item.result && xType === 'time') {
@@ -84,7 +109,7 @@ const HorizontalBarChart = (props: IProps) => {
84
109
  value: item.result,
85
110
  label: label,
86
111
  itemStyle: item.style,
87
- itemId: item.itemId
112
+ itemId: item.itemId,
88
113
  }
89
114
  }
90
115
 
@@ -92,13 +117,14 @@ const HorizontalBarChart = (props: IProps) => {
92
117
  value: item.result,
93
118
  label: label,
94
119
  itemStyle: item.style,
95
- itemId: item.itemId
120
+ itemId: item.itemId,
96
121
  }
97
122
  })
98
123
  )
99
124
 
100
- const yData = reverse(data.map((item: EntryData) => item.label))
101
- const domain = { min: 0, max: Math.max(...data.map(item => item.result)) }
125
+ const yData = data.map((item: EntryData) => item.label)
126
+
127
+ const domain = { min: 0, max: Math.max(...data.map((item) => item.result)) }
102
128
  const backgroundBar = data.map(() =>
103
129
  xComplement === '%' ? 100 : getDomain(domain)
104
130
  )
@@ -107,9 +133,10 @@ const HorizontalBarChart = (props: IProps) => {
107
133
  const { label, result } = tooltipProps
108
134
  const { name, value } = chartValues[1]
109
135
 
110
- const dataValue = xType === 'time'
111
- ? timeConvert(value) + 'h'
112
- : takeLabelComplement(Number(value), xComplement)
136
+ const dataValue =
137
+ xType === 'time'
138
+ ? timeConvert(value) + 'h'
139
+ : takeLabelComplement(Number(value), xComplement)
113
140
 
114
141
  return `${label}: ${name} <br>` + `${result}: ${dataValue} <br>`
115
142
  }
@@ -122,15 +149,17 @@ const HorizontalBarChart = (props: IProps) => {
122
149
  : takeLabelComplement(Number(value), xComplement)
123
150
  }
124
151
 
125
- const myTool = toolboxTooltip && toolboxTooltip.saveAsImageWithTitle && {
152
+ const myTool = toolboxTooltip &&
153
+ toolboxTooltip.saveAsImageWithTitle && {
126
154
  myTool: getSaveAsImageWithTitle(
127
155
  toolboxTooltip.saveAsImageWithTitle,
128
156
  handleShowTitle
129
- )
157
+ ),
130
158
  }
131
159
 
132
- const saveAsImage = toolboxTooltip && toolboxTooltip.saveAsImage && {
133
- saveAsImage: getSaveAsImage(toolboxTooltip.saveAsImage)
160
+ const saveAsImage = toolboxTooltip &&
161
+ toolboxTooltip.saveAsImage && {
162
+ saveAsImage: getSaveAsImage(toolboxTooltip.saveAsImage),
134
163
  }
135
164
 
136
165
  const toolbox = toolboxTooltip && {
@@ -139,14 +168,14 @@ const HorizontalBarChart = (props: IProps) => {
139
168
  feature: {
140
169
  ...myTool,
141
170
  ...saveAsImage,
142
- dataView: toolboxTooltip.dataView &&
143
- getDataView(toolboxTooltip.dataView)
144
- }
171
+ dataView: toolboxTooltip.dataView && getDataView(toolboxTooltip.dataView),
172
+ },
145
173
  }
174
+
146
175
  const options = {
147
176
  grid: {
148
177
  containLabel: true,
149
- ...gridProps
178
+ ...gridProps,
150
179
  },
151
180
  series: [
152
181
  {
@@ -163,9 +192,9 @@ const HorizontalBarChart = (props: IProps) => {
163
192
  color: '#ececec',
164
193
  barBorderRadius: showTickInfos ? 0 : 10,
165
194
  opacity: showTickInfos && 0.5,
166
- borderColor: showTickInfos ? undefined : props.color
167
- }
168
- }
195
+ borderColor: showTickInfos ? undefined : props.color,
196
+ },
197
+ },
169
198
  },
170
199
  {
171
200
  xAxisIndex: 0,
@@ -175,7 +204,7 @@ const HorizontalBarChart = (props: IProps) => {
175
204
  barMaxWidth: !showTickInfos && 20,
176
205
  itemStyle: {
177
206
  color: color,
178
- barBorderRadius: showTickInfos ? 0 : 10
207
+ barBorderRadius: showTickInfos ? 0 : 10,
179
208
  },
180
209
  label: {
181
210
  formatter: formatLabel,
@@ -183,19 +212,19 @@ const HorizontalBarChart = (props: IProps) => {
183
212
  fontSize: showTickInfos ? 14 : 11,
184
213
  fontWeight: '400' as const,
185
214
  color: 'black',
186
- show: true
187
- }
188
- }
215
+ show: true,
216
+ },
217
+ },
189
218
  ],
190
219
  xAxis: {
191
220
  max: xComplement === '%' ? 100 : getDomain(domain),
192
221
  type: 'value' as const,
193
222
  data: xData,
194
223
  axisTick: {
195
- show: showTickInfos || false
224
+ show: showTickInfos || false,
196
225
  },
197
226
  axisLine: {
198
- show: showTickInfos || false
227
+ show: showTickInfos || false,
199
228
  },
200
229
  axisLabel: {
201
230
  rotate: rotateLabel,
@@ -203,37 +232,44 @@ const HorizontalBarChart = (props: IProps) => {
203
232
  formatter: (item: string) =>
204
233
  xType === 'time'
205
234
  ? timeConvert(Number(item)) + 'h'
206
- : item + xComplement
235
+ : item + xComplement,
207
236
  },
208
237
  splitLine: {
209
238
  show: showTickInfos || false,
210
239
  lineStyle: {
211
240
  type: 'dotted' as const,
212
- opacity: 0.8
213
- }
214
- }
241
+ opacity: 0.8,
242
+ },
243
+ },
215
244
  },
216
245
  yAxis: {
217
246
  data: yData,
218
247
  type: 'category' as const,
248
+ inverse: true,
219
249
  axisLine: {
220
- show: showTickInfos || false
250
+ show: showTickInfos || false,
221
251
  },
222
252
  axisLabel: {
223
- formatter: (text: string) => truncateLabel(text, labelWordSize),
224
- fontWeight: boldTickLabel ? '400' as const : undefined
253
+ formatter: (text: string) =>
254
+ data.find(item => item.image)
255
+ ? formatLabelWithImage(text)
256
+ : truncateLabel(text, labelWordSize),
257
+ max: 10,
258
+ margin: 12,
259
+ fontWeight: boldTickLabel ? ('400' as const) : undefined,
260
+ rich: Object.assign({}, ...richData),
225
261
  },
226
262
  axisTick: {
227
263
  show: showTickInfos || false,
228
- alignWithLabel: true
264
+ alignWithLabel: true,
229
265
  },
230
266
  splitLine: {
231
267
  show: showTickInfos || false,
232
268
  lineStyle: {
233
269
  type: 'dotted' as const,
234
- opacity: 0.8
235
- }
236
- }
270
+ opacity: 0.8,
271
+ },
272
+ },
237
273
  },
238
274
  title: {
239
275
  left: marginLeftTitle || '5.9%',
@@ -243,21 +279,21 @@ const HorizontalBarChart = (props: IProps) => {
243
279
  textStyle: {
244
280
  fontFamily: 'Roboto, Helvetica, Arial, sans-serif',
245
281
  fontSize: titleFontSize || 16,
246
- fontWeight: '400' as const
247
- }
282
+ fontWeight: '400' as const,
283
+ },
248
284
  },
249
285
  tooltip: tooltipProps && {
250
286
  trigger: 'axis' as const,
251
287
  axisPointer: {
252
288
  type: 'shadow' as const,
253
289
  shadowStyle: {
254
- opacity: 0.5
255
- }
290
+ opacity: 0.5,
291
+ },
256
292
  },
257
293
  formatter: formatTooltip,
258
- textStyle: { fontSize: 11.5 }
294
+ textStyle: { fontSize: 11.5 },
259
295
  },
260
- toolbox
296
+ toolbox,
261
297
  }
262
298
 
263
299
  const clickEvent = { click: onClickBar }