简体   繁体   English

Highcharts:如何将每列显示为单独的系列?

[英]Highcharts: How to display each column as a separate series?

I'm trying to display the data from my json. 我正在尝试显示json中的数据。

My json file looks like this: 我的json文件如下所示:

0: {OGS: "26", STRM: "1811", ACAD_CAREER: null, YEAR: "2018"}1: {OGS: "4144", STRM: "1801", ACAD_CAREER: null, YEAR: "2018"}2: {OGS: "3935", STRM: "1802", ACAD_CAREER: null, YEAR: "2018"}3: {OGS: "16", STRM: "1812", ACAD_CAREER: null, YEAR: "2018"}length: 4__proto__: Array(0)

And here's my code: 这是我的代码:

<script>
$(function () {

var categData = [];
var statusACountData = [];
var statusBCountData = [];
var dateVal=[];
var statusVal=[];
var countVal = [];

    var jsonvar = [];
    $.getJSON('http://localhost:37590/get_OGSDataPerTermYear/ORT/2018', function (jsonData) {


               for(i=0;i<jsonData.length;i++){
               dateVal[i]=jsonData[i].STRM;
              // statusVal[i]=jsonData[i].status;
               countVal[i]=jsonData[i].OGS;                   
            }

    //    jsonData
             console.log(jsonData);
             console.log(countVal);
             console.log(categData);


  //  $(function () {
        Highcharts.chart('container', {
            chart: {
                type: 'column'
            },

            xAxis: {
                categories: dateVal,
                crosshair: true
            },
            series: [{
                name: dateVal,
                data: countVal
            }]
        });

      //  });
         });
});
</script>

The column is not showing and the 4 data which is: 1801 1802 1803 and 1804 only appears in 1 series. 该列未显示,四个数据:1801 1802 1803和1804仅以1系列出现。 I want to make them each series with different data which is OGS number. 我想让他们每个系列都有不同的数据,这些数据是OGS编号。

enter image description here Your data is not in right format. 在此处输入图像描述您的数据格式不正确。 If you need to plot STRM is x axis , you will need to put them in a separate array and assign it to xAxis. 如果需要将STRM绘制为x轴,则需要将它们放置在单独的数组中并将其分配给xAxis。

The yAxis also accepts an array of objects and the object need to have two keys like name & data where data is an array . yAxis还接受一个对象数组,并且该对象需要具有两个键,例如namedata ,其中data是一个数组。

Also OGS values are in string , so adding a + before jsonData[i].OGS will convert it to number. OGS值也在string中,因此在jsonData[i].OGS之前添加+会将其转换为数字。

 let jsonData = [{ OGS: "2456", STRM: "1811", ACAD_CAREER: null, YEAR: "2018" }, { OGS: "4144", STRM: "1801", ACAD_CAREER: null, YEAR: "2018" }, { OGS: "3935", STRM: "1802", ACAD_CAREER: null, YEAR: "2018" }, { OGS: "2316", STRM: "1812", ACAD_CAREER: null, YEAR: "2018" }] var categData = []; var statusACountData = []; var statusBCountData = []; var dateVal = []; var statusVal = []; var countVal = []; var jsonvar = []; for (let i = 0; i < jsonData.length; i++) { dateVal[i] = jsonData[i].STRM; countVal.push({ name: 'someText', data: [+jsonData[i].OGS] }) } Highcharts.chart('container', { chart: { type: 'column' }, title: { text: 'Some Text' }, subtitle: { text: 'someTExt' }, xAxis: { categories: dateVal, crosshair: true }, yAxis: { min: 0, title: { text: 'OGS' } }, tooltip: { headerFormat: '<span style="font-size:10px">{point.key}</span><table>', footerFormat: '</table>', shared: true, useHTML: true }, plotOptions: { column: { pointPadding: 0.2, borderWidth: 0 } }, series: countVal }); 
 <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 

To make it as you expect (each column is a separate series) you can follow this approach: 为了达到您的期望(每列都是一个单独的系列),您可以采用以下方法:

  1. Prepare series and xAxis.categories arrays. 准备seriesxAxis.categories数组。 Each series.data array should have an appropriate amount of null points before the one with real value. 每个series.data数组在具有实际值的空点之前应具有适当数量的空点。 Something like that: 像这样:

categories: 类别:

["1811", "1801", "1802", "1812"]

series: 系列:

[{
  name: "cat - 0",
  data: [2456]
}, {
  name: "cat - 1",
  data: [null, 4144]
}, {
  name: "cat - 2",
  data: [null, null, 3935]
}, {
  name: "cat - 3",
  data: [null, null, null, 2316]
}]
  1. plotOptions.column.grouping should be disabled: plotOptions.column.grouping应该被禁用:

Highcharts.chart('container', {
  chart: {
    type: 'column'
  },
  xAxis: {
    crosshair: true,
    categories: categories
  },
  plotOptions: {
    column: {
      grouping: false
    }
  },
  series: series
});

Demo: 演示:

API reference: API参考:

can you guys check my code for drilldown? 你们可以检查我的代码以进行深入研究吗? it only display 1 data series right after i clicked the column on the first chart,. 在我单击第一个图表上的列之后,它仅显示1个数据系列。

 $.getJSON('http://localhost:37590/get_OGSDataPerTermYear/' + strCampus + "/" + 
 data[i].YEAR, function (jsonDataDrill) {

            const datadrill = jsonDataDrill
            console.log(jsonDataDrill);

            for (d = 0; d < datadrill.length; d++) {
                categories = datadrill[d].STRM

            dataseries.push({
                id: datadrill[d].YEAR,
                name: +datadrill[d].STRM,
                data: [{
                    y: +datadrill[d].OGS
                }]
                })
            }
        });

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

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