简体   繁体   English

使用 Object 方法将 json 数据重新格式化为 highcharts

[英]Reformat parsing of json data into highcharts using Object method

I know just a little React but have been tasked with displaying json using highcharts. The json is returned via a fetch api call and looks like below.我只知道一点 React,但我的任务是使用 highcharts 显示 json。json 通过调用 api 返回,如下所示。 I currently use the code at bottom and it works fine.我目前使用底部的代码并且工作正常。 However, it is hard-coded and I would like to refactor/improve it, maybe looping over the keys object. Research shows I could potentially use .entries or .map , but examples I find call those functions in the render section, but with the way my code it, I need to do it in getConfig() and componentDidMount() .但是,它是硬编码的,我想重构/改进它,也许循环键 object。研究表明我可能会使用.entries.map ,但我发现的示例在渲染部分调用这些函数,但使用按照我的编码方式,我需要在getConfig()componentDidMount()中完成。

Suggestions?建议?

JSON to be displayed using highcharts JSON 使用 highcharts 显示

[
    {
        "reason": "reason-1", 
        "fq_counts": {"FQ1": 35, "FQ4": 92, "FQ3": 91, "FQ2": 49}
    }, 
    {
        "reason": "reason-2", 
        "fq_counts": {"FQ1": 53, "FQ4": 32, "FQ3": 82, "FQ2": 16}
    }, 
    // etc...
]

Metrics.jsx指标.jsx

import HighCharts from "highcharts"
import HighchartsReact from "highcharts-react-official";
// other imports...

const getConfig = ( metricsData ) => ({
    chart: {
        type: 'column'
    },
    title: {
        text: 'my title'
    },
    
    // other highcharts objects...
    
    series: [
        {
            name: metricsData["reason0"],
            data: [
                metricsData["data0Label0"], 
                metricsData["data0Label1"], 
                metricsData["data0Label2"], 
                metricsData["data0Label3"], 
            ]
        },{
            name: metricsData["reason1"],
            data: [
                metricsData["data1Label0"], 
                metricsData["data1Label1"], 
                metricsData["data1Label2"], 
                metricsData["data1Label3"], 
            ]
        },
        // more objects...
    ]
});


class Metrics extends Component {

    constructor() {
      super();
      this.state = {
          metricsData: {},
        }
    }
    
    componentDidMount() {
        fetch(process.env.MY_ENDPOINT + '/metrics', {credentials: 'include'})
        .then(res => res.json())
        .then(data => {
            const keys = Object.keys(data[0].fq_counts);
            this.setState({
                metricsData: {
                
                    "reason0":data[0].reason,
                    "data0Label0":data[0].fq_counts[keys[0]],
                    "data0Label1":data[0].fq_counts[keys[1]],
                    "data0Label2":data[0].fq_counts[keys[2]],
                    "data0Label3":data[0].fq_counts[keys[3]],
                    
                    "reason1":data[1].reason,
                    "data1Label0":data[1].fq_counts[keys[0]],
                    "data1Label1":data[1].fq_counts[keys[1]],
                    "data1Label2":data[1].fq_counts[keys[2]],
                    "data1Label3":data[1].fq_counts[keys[3]],
                    
                    // more data...
                   
                }
            });
        });
    }  
  
    render() {
      const { metricsData } = this.state;
      const chartConfig = getConfig(metricsData);
        return (
            <Paper sx={{width: 1}}>
                <Box
                    sx={{
                    display: 'flex',
                    flexDirection: 'column',
                        alignItems: 'flex-start',
                    p: 1,
                    m: 1,
                    bgcolor: 'background.paper',
                    borderRadius: 1,
                    }}
                    <Typography variant="h4" gutterBottom>
                        <b>Quarterly Metrics</b>
                    </Typography>
                            <Divider />
                    <Typography variant="p" gutterBottom>Totals</Typography>
                    <HighchartsReact highcharts={HighCharts} options={chartConfig}/>
                </Box>
            </Paper>
        );
    }
}
export default Metrics;

You can relatively easily convert your data to the Highcharts series structure.您可以相对轻松地将数据转换为 Highcharts 系列结构。 The below concept will be the same for React and pure JS:以下概念对于 React 和纯 JS 是相同的:

const keys = Object.keys(responseData[0].fq_counts);
const series = [];

responseData.forEach((dataEl, index) => {
    series.push({ name: dataEl.reason, data: [] });
    keys.forEach(key => {
        series[index].data.push([key, dataEl.fq_counts[key]]);
    });
});

Live demo: http://jsfiddle.net/BlackLabel/deskutc4/现场演示: http://jsfiddle.net/BlackLabel/deskutc4/

API Reference: https://api.highcharts.com/highcharts/series.column.data API 参考: https://api.highcharts.com/highcharts/series.column.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM