简体   繁体   English

如果值为0,Highcharts不应绘制图形

[英]Highcharts should not draw graph if the value is 0

I am using Highcharts and it reports for the whole year. 我正在使用Highcharts,并且会报告整个年度。

I have data only until July (As it is July from today) but when I plot the graph it plots until December with value as 0 (Aug to Dec). 我只有直到七月的数据(因为今天是从今天的七月),但是当我绘制图形时,它一直绘制到12月,其值为0(八月到十二月)。

I don´t want to plot the graph until Dec and I want to plot only until July. 我不想在12月之前绘制图表,而只希望在7月之前绘制图表。

 $(function() { var colors = [ '#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92' ]; var applyGradient = function(color) { return { radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 }, stops: [ [0, color], [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] ] }; }; $('#container').highcharts({ colors: colors, title: { text: 'Points with zero value are not connected by line' }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], offset: 0, }, plotOptions: { series: { connectNulls: true } }, yAxis: { min: 0, }, series: [{ data: [2, 10, 40, 40, 40, 40, 30, 0, 0, 0, 0, 0] }, ] }); colors = $.map(colors, applyGradient); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="http://code.highcharts.com/stock/highstock.js"></script> <div id="container" style="height: 300px"></div> 

In real time am getting the values from the SQL query and for this fiddle I just kept the static values 实时从SQL查询中获取值,为此,我只保留了静态值

To do something like this, you can either return only the data you require from the database or process it on the fly in JavaScript. 为此,您可以只返回数据库中所需的数据,也可以使用JavaScript快速处理这些数据。

The following takes the data and category data and processes it outside of the actual initialisation of the chart: 下面将获取数据和类别数据,并在图表的实际初始化之外进行处理:

//Original values
var categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var data = [2, 10, 40, 40, 40, 40, 30, 0, 0, 0, 0, 0]

//New arrays to store data to be displayed
var currentCategories = []
var currentData = [];

//Get current month to provide a cut-off
var currentMonth = new Date().getMonth();

//Loop through the data and add the value for each month to the data array and category array
for(var i = 0; i < categories.length; i++){
    if(currentMonth >= i){//Alternatively at this point you could exclude the values if data[i] == 0
        currentCategories.push(categories[i]);
        //Arrays may be different lengths.
        if(data.length >= (i+1)){
            currentData.push(data[i]);
        }
    }
}

While you can just exclude 0 values. 虽然您只能排除0个值。 You might lose months earlier in the year if they have a 0 value. 如果它们的值为0,则可能会在一年中的前几个月损失。 With the data you've provided it feels less risky to remove only values that are after the current month. 使用您提供的数据,仅删除当月之后的值的风险较小。

You can then use these created variables to set your data in your HighChart: 然后,您可以使用这些创建的变量在HighChart中设置数据:

$('#container').highcharts({
    colors: colors,
    title: {
      text: 'Points with zero value are not connected by line'
    },
    xAxis: {
      categories: currentCategories, //Use processed list of categories
      offset: 0,
    },

    plotOptions: {
      series: {
        connectNulls: true
      }
    },
    yAxis: {
      min: 0,
    },
    series: [{
        data: currentData //Use processed array of data
      },
    ]
  });

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

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