PromucFlow_constructor/app/client/src/components/designSystems/appsmith/ChartComponent.tsx

119 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-03-13 12:06:41 +00:00
import React from "react";
import { ChartType, ChartData } from "widgets/ChartWidget";
import styled from "styled-components";
import { invisible } from "constants/DefaultTheme";
import _ from "lodash";
/*
2020-03-13 12:06:41 +00:00
import ReactFC from "react-fusioncharts";
import FusionCharts from "fusioncharts";
import Column2D from "fusioncharts/fusioncharts.charts";
import FusionTheme from "fusioncharts/themes/fusioncharts.theme.fusion";
ReactFC.fcRoot(FusionCharts, Column2D, FusionTheme);
*/
const FusionCharts = require("fusioncharts");
const Charts = require("fusioncharts/fusioncharts.charts");
const FusionTheme = require("fusioncharts/themes/fusioncharts.theme.fusion");
Charts(FusionCharts);
FusionTheme(FusionCharts);
2020-03-13 12:06:41 +00:00
export interface ChartComponentProps {
chartType: ChartType;
chartData: ChartData[];
xAxisName: string;
yAxisName: string;
chartName: string;
isVisible?: boolean;
}
const CanvasContainer = styled.div<ChartComponentProps>`
border: none;
border-radius: ${props => `${props.theme.radii[1]}px`};
height: 100%;
width: 100%;
background: white;
box-shadow: 0 1px 1px 0 rgba(60,75,100,.14),0 2px 1px -1px rgba(60,75,100,.12),0 1px 3px 0 rgba(60,75,100,.2);
position: relative;
${props => (!props.isVisible ? invisible : "")};
}`;
class ChartComponent extends React.Component<ChartComponentProps> {
chartInstance = new FusionCharts();
getChartType = (chartType: ChartType) => {
2020-03-13 12:06:41 +00:00
switch (chartType) {
case "LINE_CHART":
return "line";
case "BAR_CHART":
return "bar2d";
case "PIE_CHART":
return "pie2d";
case "COLUMN_CHART":
return "column2d";
case "AREA_CHART":
return "area2d";
default:
return "column2d";
}
};
getChartData = (chartData: ChartData[]) => {
2020-03-13 12:06:41 +00:00
return chartData.map(item => {
return {
label: item.x,
value: item.y,
};
});
};
createGraph = () => {
const chartConfig = {
type: this.getChartType(this.props.chartType),
renderAt: "chart-container",
width: "100%",
height: "100%",
dataFormat: "json",
dataSource: {
chart: {
caption: this.props.chartName,
xAxisName: this.props.xAxisName,
yAxisName: this.props.yAxisName,
theme: "fusion",
},
data: this.getChartData(this.props.chartData),
},
};
this.chartInstance = new FusionCharts(chartConfig);
};
componentDidMount() {
this.createGraph();
FusionCharts.ready(() => {
this.chartInstance.render();
});
}
componentDidUpdate(prevProps: ChartComponentProps) {
if (!_.isEqual(prevProps, this.props)) {
if (prevProps.chartType !== this.props.chartType) {
const chartType = this.getChartType(this.props.chartType);
this.chartInstance.chartType(chartType);
} else {
this.chartInstance.setChartData({
2020-03-13 12:06:41 +00:00
chart: {
caption: this.props.chartName,
xAxisName: this.props.xAxisName,
yAxisName: this.props.yAxisName,
2020-03-13 12:06:41 +00:00
theme: "fusion",
},
data: this.getChartData(this.props.chartData),
});
}
}
}
render() {
return <CanvasContainer {...this.props} id="chart-container" />;
}
}
2020-03-13 12:06:41 +00:00
export default ChartComponent;