简体   繁体   English

如何使用Highcharts使用来自数据库的传入数据系列制作多个Y轴

[英]How to make multiple Y-axis with incoming series of data from database using Highcharts

Actually the thing is,I want a graph in which the series of data will be shown but not with single y-axis.But with multiple y-axis,let's say i have 8 series of data which will be shown in single chart with single y-axis.What I want it tobe selective ie the user will click the stream on and off to make it visible or disable it,at the same time the axis of each series of data will also get visible. 实际上,我想要一个图形,其中将显示一系列数据,但不显示单个y轴。但是对于多个y轴,假设我有8个系列的数据,这些数据将在单个图表中显示,单个y轴。我希望它具有选择性,即用户将打开和关闭流以使其可见或禁用它,与此同时,每个数据系列的轴也将变为可见。 Here is the link of desired graph(the graph what i want actually). 这是所需图形的链接(图形实际上是我想要的)。

`






<!-- Styles -->
<style>
#chartdiv {
    width   : 140%;
    height  : 800px;
}
.highcharts-credits {

    display: none !important;
}

</style>

<!-- Resources -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="http://highcharts.github.io/export-csv/export-csv.js"></script>

<div id="container" style="min-width: 200px; height: 400px; margin: 0 auto"></div>


<!-- Chart code --> <script>

        $(function () {
               var lastPart = window.location.pathname.split("/").pop();
                $.getJSON('/demo/Devices/chGraph/'+lastPart, function(data) {
                    // Populate series
                    Xdata = [];
                    Ydata = [];

                    Xdata =  data[0].CHGRAPHUpdatetime.time.split(",");


                    for(i = 0; i< data[0].channelGraph.length; i++) {
                       Ydata[i] = {"name": "", "data":[]};
                        Ydata[i].name = data[0].channelGraph[i].chkey;

                        var listnumber = data[0].channelGraph[i].list.split(',').map(function(item) {
                    return parseInt(item, 10);

                });
                    Ydata[i].data = listnumber




                    }
                       console.log(Ydata);


                var chart = new Highcharts.Chart({
                    chart: {
                          renderTo: container,
                          zoomType: 'xy',
                          panning: true,
                          panKey: 'shift',
                          height:600,
                          width:1000,
                          borderColor: '#EBBA95',
                          borderWidth: 4,
                          spacingTop: 30,
                          spacingBottom: 50,
                          spacingLeft: 100,
                          spacingRight:80
                    },
                    title: {

                            text: 'Monthly Average Temperature'
                           },


                    xAxis: {
                            type: 'varchar',
                            categories: Xdata
                            }, 


                    tooltip: {
                            shared: true,
                            useHTML: true,
                            headerFormat: '<small>{point.key}</small><table>',
                            pointFormat: '<tr><td style="color: {series.color}">{series.name}: </td>' +
                            '<td style="text-align: right"><b>{point.y} </b></td></tr>',
                            footerFormat: '</table>',
                            valueDecimals: 2
                            },


                    series:Ydata,


                },


        }) //end ajax call

        }) // end javascfrpt
        $('#getcsv').click(function () {
    alert(chart.getCSV());
          });   



</script>

<!-- HTML -->
<div id="chartdiv" >

</div>

how to add multiple y axis according to each series data. 如何根据每个系列数据添加多个y轴。

we get the data from a json and above code shows only one y axis. 我们从json获取数据,上面的代码仅显示一个y轴。

I have attached the image of the graph. 我已附上图表的图像。 This is the actual graph. 这是实际的图。

following is the required graph image This is required graph. 以下是必需的图形图像这是必需的图形。

What you most likely need to do is to add the attribute yAxis to the yData array, so that the format will become: 您最可能需要做的是将yAxis属性yAxis到yData数组,这样格式将变为:

[[name: "x", data: [], yAxis: 1], [name: "y", data: [], yAxis: 2] ]

Make a counter which adds 1 to the yAxis index in each iteration so you assign it to a different yAxis. 制作一个在每次迭代时将y索引加1的计数器,以便将其分配给其他y轴。

The yAxis you need to define in the chart as well 您还需要在图表中定义的yAxis

   yAxis: [{
        title: {
            text: 'Title 1'
        }
    }, {
    title: {
            text: 'Title 2'
    }
    }, {
    title: {
            text: 'Title 3'
    },
    }],

You can find more information about this in this Highcharts demo 您可以在此Highcharts演示中找到有关此信息的更多信息

What you want is possible using highcharts here is and example Multiple axes . 您可以使用此处的高图表来实现所需的示例,例如多轴

Basically you specify value of yAxis property as a list of objects, while creating Chart object and each object represents the kind of line will be drawn for dedicated series. 基本上,您将yAxis属性的值指定为对象列表,同时创建Chart对象并且每个对象代表将为专用系列绘制的线的种类。

yAxis: [{ // Primary yAxis
    labels: {
        format: '{value}°C',
        style: {
            color: Highcharts.getOptions().colors[2]
        }
    },
    title: {
        text: 'Temperature',
        style: {
            color: Highcharts.getOptions().colors[2]
        }
    },
    opposite: true

}, { // Secondary yAxis
    gridLineWidth: 0,
    title: {
        text: 'Rainfall',
        style: {
            color: Highcharts.getOptions().colors[0]
        }
    },
    labels: {
        format: '{value} mm',
        style: {
            color: Highcharts.getOptions().colors[0]
        }
    }

}, { // Tertiary yAxis
    gridLineWidth: 0,
    title: {
        text: 'Sea-Level Pressure',
        style: {
            color: Highcharts.getOptions().colors[1]
        }
    },
    labels: {
        format: '{value} mb',
        style: {
            color: Highcharts.getOptions().colors[1]
        }
    },
    opposite: true
}];

One more thing is to specify in series which axis to be used. 另一件事是串行指定要使用的轴。 I your case Ydata[i] is a series, it must have following structure 我的案例Ydata[i]是一个序列,它必须具有以下结构

{
    name: 'Sea-Level Pressure',
    type: 'spline',
    //number of axis to represent scale. 1 is by default for primary yAxis.
    yAxis: 2, 
    data: [1016, 1016, 1015.9, 1015.5, 1012.3, 1009.5, 1009.6, 1010.2, 1013.1, 1016.9, 1018.2, 1016.7],
    marker: {
        enabled: false
    },
    dashStyle: 'shortdot',
    tooltip: {
        valueSuffix: ' mb'
    }

}

So when you get data as response generate list Ydata of objects having above structure. 因此,当您获取数据作为响应时,将生成具有上述结构的对象的列表Ydata

I hope that solves your issue. 希望能解决您的问题。 Here is a demo after adding one more series to official example. 这是在官方示例中添加了更多系列后的演示

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

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