Highcharts-同一页中多次出现同一图表

Highcharts - Same chart multiple times in same page

本文关键字:一页 Highcharts-      更新时间:2024-05-02

如何将same page中的多个图表与same data一起使用?

我正在尝试如下,但只得到第一个图表工作

<div class="areaChartTwoWay" style="min-width: 150px; height: 200px;"></div>
<div class="areaChartTwoWay" style="min-width: 150px; height: 200px;"></div>
<div class="areaChartTwoWay" style="min-width: 150px; height: 250px;"></div>

$(function () {
  // Everything in common between the charts
  var commonOptions = {
    colors: ['#fe5758', '#99cc03', '#33b5e6', '#fd8f40', '#e7ca60', '#40abaf', '#f6f7f8', '#e9e9eb'],
    chart: {
      style: {
        fontFamily: 'Roboto Light',
        fontWeight: 'normal',
        fontSize: '12px',
        color: '#585858',
      }
    },
    title: {
      text: null
    },
    subtitle: {
      text: null
    },
    credits: {
      enabled: false
    },
    exporting: {
      enabled: false
    },
    xAxis: {
      title: {
        style: {
          fontFamily: 'Roboto',
          color: "#bbb",
        }
      },
      labels: {
        style:{ color: '#bbb' }
      },
      lineColor: '#e4e4e4',
      lineWidth: 1,
      tickLength: 0,
    },
    yAxis: {
      title: {
        style: {
          fontFamily: 'Roboto',
          color: "#bbb",
        }
      },
      offset:-6,
      labels: {
        style:{ color: '#bbb' }
      },
      tickInterval: 100,
      lineColor: '#e4e4e4',
      lineWidth: 1,
      gridLineDashStyle: 'dash',
    },
    series: [{
      backgroundColor: "rgba(0 ,0, 0, 0.5)",
    }],
    // Area Chart
    plotOptions: {
      area: {
        stacking: 'normal',
        lineWidth: 1,
        fillOpacity: 0.1,
        marker: {
          lineWidth: 1.5,
          symbol: 'circle',
          fillColor: 'white',
        },
        legend: {
          radius: 2,
        }
      }
    },
    tooltip: {
      useHTML: true,
      shared: true,
      backgroundColor: '#5f5f5f',
      borderWidth: 0,
      style: {
        padding:10,
        color: '#fefefe',
      }
    },
    legend: {
      itemStyle: {
        fontFamily: 'Roboto Light',
        fontWeight: 'normal',
        fontSize: '12',
        color: '#666666',
      },
      marker: {
        symbol: 'square',
        verticalAlign: 'middle',
        radius: '4',
      },
      symbolHeight: 6,
      symbolWidth: 6,
    },
  };
  $('.areaChartTwoWay').highcharts(Highcharts.merge(commonOptions, {
    chart: { type: 'area' },
    xAxis: {
      title: { text: 'Days', },
      text: 'Date',
      type: 'datetime',
      dateTimeLabelFormats: {
        day: '%eth %b'
      },
    },
    yAxis: {
      title: { text: 'Count', },
    },
    series: [{
      name: 'Legend 2',
      lineColor: '#fb8b4b',
      marker: { lineColor: '#fb8b4b', fillColor: 'white', },
      data: [150, 105, 130, 160, 185, 280, 185, 160, 135, 200],
      legendIndex:1,
      pointStart: Date.UTC(2016, 2, 11),
      pointInterval: 24 * 3600 * 1000 // one day
    }, {
      name: 'Legend 1',
      lineColor: '#99cc03',
      marker: { lineColor: '#99cc03', fillColor: 'white', },
      data: [82, 68, 90, 69, 75, 62, 46, 36, 79, 34],
      legendIndex:0,
      pointStart: Date.UTC(2016, 2, 11),
      pointInterval: 24 * 3600 * 1000 // one day
    }]
  }));
  $('.highcharts-grid > path:last-child').remove();
  $('.highcharts-markers > path:last-child').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div class="areaChartTwoWay" style="min-width: 150px; height: 200px;"></div>
<div class="areaChartTwoWay" style="min-width: 150px; height: 200px;"></div>
<div class="areaChartTwoWay" style="min-width: 150px; height: 250px;"></div>

尝试在每个(..)中移动高点调用,如下所示:

$(function () {
  // Everything in common between the charts
  var commonOptions = {
    colors: ['#fe5758', '#99cc03', '#33b5e6', '#fd8f40', '#e7ca60', '#40abaf', '#f6f7f8', '#e9e9eb'],
    chart: {
      style: {
        fontFamily: 'Roboto Light',
        fontWeight: 'normal',
        fontSize: '12px',
        color: '#585858',
      }
    },
    title: {
      text: null
    },
    subtitle: {
      text: null
    },
    credits: {
      enabled: false
    },
    exporting: {
      enabled: false
    },
    xAxis: {
      title: {
        style: {
          fontFamily: 'Roboto',
          color: "#bbb",
        }
      },
      labels: {
        style:{ color: '#bbb' }
      },
      lineColor: '#e4e4e4',
      lineWidth: 1,
      tickLength: 0,
    },
    yAxis: {
      title: {
        style: {
          fontFamily: 'Roboto',
          color: "#bbb",
        }
      },
      offset:-6,
      labels: {
        style:{ color: '#bbb' }
      },
      tickInterval: 100,
      lineColor: '#e4e4e4',
      lineWidth: 1,
      gridLineDashStyle: 'dash',
    },
    series: [{
      backgroundColor: "rgba(0 ,0, 0, 0.5)",
    }],
    // Area Chart
    plotOptions: {
      area: {
        stacking: 'normal',
        lineWidth: 1,
        fillOpacity: 0.1,
        marker: {
          lineWidth: 1.5,
          symbol: 'circle',
          fillColor: 'white',
        },
        legend: {
          radius: 2,
        }
      }
    },
    tooltip: {
      useHTML: true,
      shared: true,
      backgroundColor: '#5f5f5f',
      borderWidth: 0,
      style: {
        padding:10,
        color: '#fefefe',
      }
    },
    legend: {
      itemStyle: {
        fontFamily: 'Roboto Light',
        fontWeight: 'normal',
        fontSize: '12',
        color: '#666666',
      },
      marker: {
        symbol: 'square',
        verticalAlign: 'middle',
        radius: '4',
      },
      symbolHeight: 6,
      symbolWidth: 6,
    },
  };
  $('.areaChartTwoWay').each(function() {
$(this).highcharts(Highcharts.merge(commonOptions, {
    chart: { type: 'area' },
    xAxis: {
      title: { text: 'Days', },
      text: 'Date',
      type: 'datetime',
      dateTimeLabelFormats: {
        day: '%eth %b'
      },
    },
    yAxis: {
      title: { text: 'Count', },
    },
    series: [{
      name: 'Legend 2',
      lineColor: '#fb8b4b',
      marker: { lineColor: '#fb8b4b', fillColor: 'white', },
      data: [150, 105, 130, 160, 185, 280, 185, 160, 135, 200],
      legendIndex:1,
      pointStart: Date.UTC(2016, 2, 11),
      pointInterval: 24 * 3600 * 1000 // one day
    }, {
      name: 'Legend 1',
      lineColor: '#99cc03',
      marker: { lineColor: '#99cc03', fillColor: 'white', },
      data: [82, 68, 90, 69, 75, 62, 46, 36, 79, 34],
      legendIndex:0,
      pointStart: Date.UTC(2016, 2, 11),
      pointInterval: 24 * 3600 * 1000 // one day
    }]
  }))});
  $('.highcharts-grid > path:last-child').remove();
  $('.highcharts-markers > path:last-child').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div class="areaChartTwoWay" style="min-width: 150px; height: 200px;"></div>
<div class="areaChartTwoWay" style="min-width: 150px; height: 200px;"></div>
<div class="areaChartTwoWay" style="min-width: 150px; height: 250px;"></div>