python - 使用 python 3 将系列动态添加到 highcharts

标签 python json python-3.x highcharts

我无法从基于 CSV 文件的 panda 数据框架在 High 图表中添加动态系列。

CSV 文件中的数据如下所示。

    Installation    Manufacturing   Sales & Distribution    Project Development Other
          43,934           24,916                 11,744        -               12,908 
          52,503           24,064                 17,722        -               5,948 
          57,177           29,742                 16,005        7,988           8,105 
          69,658           29,851                 19,771        12,169          11,248 
          97,031           32,490                 20,185        15,112          8,989 
          119,931          30,282                 24,377        22,452          11,816 
          137,133          38,121                 32,147        34,400          18,274 
          154,175          40,434                 39,387        34,227          18,111 

我正在使用的代码

import pandas as pd 
import csv
from highcharts import Highchart

FileName = "C:/Users/kumarso/Documents/Pthon/Pthon.csv"
FileName1 = "C:/ExcelP/Highcharts_Test.html"

H = Highchart()


data = pd.read_csv(FileName) 


for i, series in enumerate(data):
    H.add_data_set(series,
                    series_type='line',
                    name= data.columns)

# write out the html
with open(FileName1, "wt") as fh:
    fh.write(H.htmlcontent)

当我打开 Html 文件查看图表时,它没有显示任何值、图例等,仅显示一个空图表。我遇到的问题是如何使用标题添加这样的系列。

series: [{
        name: 'Installation',
        data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
    }, {
        name: 'Manufacturing',
        data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
    }, {
        name: 'Sales & Distribution',
        data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
    }, {
        name: 'Project Development',
        data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
    }, {
        name: 'Other',
        data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
    }],

编辑 1:图表的结果应如下所示

enter image description here

图表系列在源代码中如下所示。它不需要数据。

var chart = new Highcharts.Chart(option);

            var data = [{"data": "Installation", "type": "line", "name": "Series 1"}, {"data": "Manufacturing ", "type": "line", "name": "Series 2"}, {"data": "Sales & Distribution", "type": "line", "name": "Series 3"}, {"data": "Project Development", "type": "line", "name": "Series 4"}, {"data": "Other", "type": "line", "name": "Series 5"}];
            var dataLen = data.length;
            for (var ix = 0; ix < dataLen; ix++) {
                chart.addSeries(data[ix]);
            }

最佳答案

我遇到了一个非常相似的问题,并使用您的帖子作为引用来解决,调整您的代码,对您来说它应该可以这样工作:

for series in enumerate(data):
   for serie in series: 
       H.add_data_set(serie.data,
                series_type='line',
                name= serie.name)

希望有帮助。

关于python - 使用 python 3 将系列动态添加到 highcharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125367/

相关文章:

Javascript 自定义索引访问器

python - 基于 JSON 文件创建 DataFrame 时 Spark SQL "No input paths specified in jobs"

python - 等同于'execfile'的Python 3.3

python - 为数据分配编号

python - 从 Rails 服务器排队 Python 后台作业

python - ""的含义?成员资格测试空字符串文字

python - 用特定字符替换字符串中的索引范围 - Python

python - 如何在 DataFrame 上执行 concat 时分配默认值

javascript - 使用 JsonPipe 纠正 Angular 中的缩进 json

python - 在Makefile中使用空格干净地处理文件路径