在Highchart中给线下方的区域上色。

huangapple go评论91阅读模式
英文:

Color the area below the line in a highchart

问题

Here's the translated part of the content you provided:

我需要修改一个线状图表(我没有创建它)。该应用程序从数据库中提取数据,并在将其添加到图表之前按小时对每个元素进行排序。当你悬停在一个点上时,应用程序看起来像这样:

我的任务是用不同的颜色(就像面积图表)填充下面的区域:

这是生成图表的代码:

var chart = Highcharts.stockChart('production-time-chart', {
    // 其他不相关的配置...
    chart: {
        type: 'areaspline', // 这里的类型设置为'areaspline'
        zoomType: 'xy',
        plotBorderWidth: 2,
        // 其他配置...
    },
    // 其他配置...
});

这是用于添加系列的代码:

chart.addSeries({
    color: '#2F86A6',
    lineWidth: 3,
    name: name,
    keys: ['x', 'y', 'name', 'selectable', 'partnumber', 'rawObject', 'marker.lineColor', 'marker.lineWidth'],
    data: data,
    showInNavigator: true,
});

我的目标是在点下面添加颜色区域,但保留样式和功能性。如果您需要更多信息,请告诉我。

英文:

I need to modify a line chart (that I didn't create). The app extracts data from a database and order each element by hour before adding it to the chart. When you hover a point, the app look like this:

在Highchart中给线下方的区域上色。

My task is to paint the below area with a different color (like an area chart):

在Highchart中给线下方的区域上色。

This is the code that generates the chart:

var chart = Highcharts.stockChart('production-time-chart', {
    legend: {
        /*Not relevant (i think)*/
    },
    time: {
        timezoneOffset: new Date(Date.now()).getTimezoneOffset(),
    },
    rangeSelector: {
        buttonSpacing: 10,
        buttonTheme: {
            width: 120
        },
        inputDateFormat: '%H:%M:%S',
        buttons: /*Not relevant (i think)*/,
        selected: currentNavigatorButton,
    },
    chart: {
        type: 'areaspline',
        zoomType: 'xy',
        plotBorderWidth: 2,
        events: {
            click: () => clearPoints(),
            selection: function (e) {
                clearPoints();
                if (e.xAxis.length > 0 && e.yAxis.length > 0) {
                    var models = [];
                    var chartObj = this,
                        xMin = e.xAxis[0].min,
                        xMax = e.xAxis[0].max,
                        yMin = e.yAxis[0].min,
                        yMax = e.yAxis[0].max;

                    if (chartObj.series[0]) {
                        chartObj.series[0].data.forEach((elem) => {
                            if (elem.x >= xMin && elem.x <= xMax && elem.y >= yMin && elem.y <= yMax)
                                if (elem.options.selectable) {                                    
                                    //selectPoint(elem);
                                    models.push(elem.options);
                                }                            
                        });
                    }                    
                    evalPointsChart(models);
                }
                return false;
            },
            redraw: function () {                
                var [min, max] = calculateNavigator();                
                chart.xAxis[0].setExtremes(min, max, false);                
            }
        },
        height: 500
    },
    yAxis: {
        title: {
            text: 'Minutos'
        },
        crosshair: true
    },
    xAxis: {
        title: {
            text: null//'Hora'
        },        
        dateTimeLabelFormats: {
            second: '%H:%M:%S',
            //day: '%e. %b'
        },
        events: /*Not relevant (i think)*/
    },
    plotOptions: {
        line: {
            point: {
                events: /*Not relevant (i think)*/
            },
            marker: /*Not relevant (i think)*/,
            allowPointSelect: true,            
        },
    },
    navigator: {
        series: {
            lineWidth: 2,
            showInNavigator: true
        },
    },
    tooltip: {
        formatter: function () {            
            return ['<b>' + toTimeStr(new Date(this.x)) + '</b>'].concat(
                this.points ?
                    this.points.map(function (point) {
                        return `<b> ${point.point.options.partnumber ? point.point.options.partnumber + " - " : ""}
                                    ${point.series.name} 
                                </b>: ${point.y} mins.`;
                    }) :
                    []
            );
        },
        split: true
    },
});

And this is the code used to add a serie:

chart.addSeries({
        color: '#2F86A6',
        lineWidth: 3,
        name: name,
        keys: ['x', 'y', 'name', 'selectable', 'partnumber', 'rawObject', 'marker.lineColor', 'marker.lineWidth'],
        data: data,
        showInNavigator: true,
    });

I look for solutions here but they either use two separate series to display 2 different charts or have a very different way to create the chart. The closer that I came to resolve the issue was adding the property "type: 'areaspline'" to the chart constructor, but it overwrites the style and hide the points:

在Highchart中给线下方的区域上色。

My goal is to add color to the area below the points, but preserving the style and functionality. Tell me if you need more info.

答案1

得分: 2

Instead of areaspline, you need to use area series type and change plotOptions.line to plotOptions.area or plotOptions.series. For example:

plotOptions: {
  series: {
    ...,
    marker: {
      enabled: true
    }
  }
}
chart.addSeries({
  type: 'area',
  ...
});

Live demo: http://jsfiddle.net/BlackLabel/ufrb273t/

Docs: https://www.highcharts.com/docs/chart-and-series-types/chart-types

英文:

Instead of areaspline, you need to use area series type and change plotOptions.line to plotOptions.area or plotOptions.series. For example:

  plotOptions: {
    series: {
      ...,
      marker: {
        enabled: true
      }
    }
  }

chart.addSeries({
	type: 'area',
	...
});

Live demo: http://jsfiddle.net/BlackLabel/ufrb273t/

Docs: https://www.highcharts.com/docs/chart-and-series-types/chart-types

huangapple
  • 本文由 发表于 2023年5月17日 09:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定