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

Javascript 如果值为0,则Highcharts不应绘制图形,javascript,php,highcharts,Javascript,Php,Highcharts,我使用Highcharts和全年的it报告 我只有7月份之前的数据,因为从今天算起是7月份,但当我绘制图表时,它会绘制到12月份,值为0 Aug-Dec 我不想在12月之前绘制图表,我只想在7月之前绘制 $function{ 变量颜色=[ ‘4572A7’, “AA4643”, “89A54E”, ‘80699B’, “3D96AE”, “DB843D”, “92A8CD”, ‘A47D7C’, “B5CA92” ]; var applyGradient=functioncolor{ 返回{

我使用Highcharts和全年的it报告

我只有7月份之前的数据,因为从今天算起是7月份,但当我绘制图表时,它会绘制到12月份,值为0 Aug-Dec

我不想在12月之前绘制图表,我只想在7月之前绘制

$function{ 变量颜色=[ ‘4572A7’, “AA4643”, “89A54E”, ‘80699B’, “3D96AE”, “DB843D”, “92A8CD”, ‘A47D7C’, “B5CA92” ]; var applyGradient=functioncolor{ 返回{ 径向梯度:{ cx:0.5, cy:0.3, r:0.7 }, 停止:[ [0,颜色], [1,Highcharts.Colorcolor.Brighlight-0.3.获取“rgb”] ] }; }; $'container'.highcharts{ 颜色:颜色, 标题:{ 文本:“值为零的点不通过直线连接” }, xAxis:{ 类别:[一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月], 偏移量:0, }, 打印选项:{ 系列:{ connectNulls:true } }, 亚克斯:{ 分:0,, }, 系列:[{ 数据:[2,10,40,40,40,40,30,0,0,0,0,0] }, ] }; 颜色=$.MapColor,applyGradient; };
要做到这一点,您可以只从数据库返回所需的数据,也可以使用JavaScript动态处理数据

以下内容获取数据和类别数据,并在图表实际初始化之外对其进行处理:

//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]);
        }
    }
}
$('#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
      },
    ]
  });