javascript - react-highcharts 折线图的渐变填充

标签 javascript reactjs charts highcharts react-highcharts

我使用 react-highcharts 创建了一个折线图。这就是它的样子:lineChart .现在,我正在尝试在线下添加颜色填充渐变,这就是我正在做的:

render() {
  let config = {
    chart: {
      animation: {
        duration: 1000
      }
    },
    plotOptions: {
      area: {
        lineWidth: 1,
        marker: {
          enabled: false,
          states: {
            hover: {
              enabled: true,
              radius: 5
            }
          }
        },
        shadow: false,
        states: {
          hover: {
            lineWidth: 1
          }
        }
      }
    },
    rangeSelector: {
      buttons: [{
        type: 'month',
        count: 1,
        text: '1m'
      }, {
        type: 'month',
        count: 3,
        text: '3m'
      }, {
        type: 'month',
        count: 6,
        text: '6m'
      }, {
        type: 'year',
        count: 1,
        text: '1y'
      }, {
        type: 'all',
        text: 'All'
      }],
      selected: 0,
      inputEnabled: false,
    },
    title: {
      text: 'Progress Chart'
    },
    series: [{
      name: 'Account Balance',
      data: this.getProgressData(),
      type: 'area',
      fillColor: {
        linearGradient: [0, 0, 0, 300],
        stops: [
          [0, '#4286f4'],
          [1, '#ffffff']
        ]
      },
      tooltip: {
        valueDecimals: 2
      }
    }],
    yAxis: { gridLineWidth: 0 },
    xAxis: { gridLineWidth: 2 },
  };

  return(
    <div>
      <ReactHighstock config={config}/>
    </div>
  )
}

我正在使用渐变,但问题是我得到的是没有长矛的平面图表,如下所示:lineChartWithGradient .我希望我的图表看起来像以前的图表,但具有渐变颜色填充。

最佳答案

请注意 x 轴上的值是如何变化的。折线图使用最小值和最大值调整极值。面积图有附加参数 threshold ( http://api.highcharts.com/highcharts/plotOptions.area.threshold ) 指定面积应该从哪里开始。默认为 0

您可以在您的数据中找到最小值并设置阈值,如下所示:

var data = [50, 71.5];
(...)
// chart options
threshold: Math.min(...data) 

实时工作示例: http://jsfiddle.net/kkulig/fznqthhw/

关于javascript - react-highcharts 折线图的渐变填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46686590/

相关文章:

javascript - 如何在 HTML 中使用类似弹出窗口的机制打开未知数量的图像

reactjs - React Semantic-ui Dropdown onChange 不起作用

ios - SciChart IOs 光标和工具提示修饰符

javascript - 从 JavaScript 加载页面

javascript - 如何将动画添加到 onmouseover javascript 函数(悬停时更改图像)?

javascript - react - 当 onClick 子项时创建一个事件类

reactjs - 状态重定向在 React Router 4.x 中不起作用

JavaFX 在两个图表中打印相同的 XYChart.Series

asp.net - ASP.NET 3.5 中的图表 : Whats the best option?

javascript - 通过 DOM 移除 HTML Table 的可见网格