简体   繁体   English

HighCharts,通过功能在页面上调用多个图表

[英]HighCharts, multiple charts on page call via function

Hopefully someone can help me as I'm stuck at the moment after searching for a solution already for the last 2 days. 希望有人可以为我提供帮助,因为在过去两天已经在寻找解决方案之后,我仍然无法解决。

I decided to look into HighCharts yesterday for a new project and it does do what I'm after. 昨天我决定研究HighCharts的一个新项目,它确实可以满足我的需求。 However I'm trying to make the implementation option into my WordPress plugin as clean as possible and that's where I'm stuck at the moment. 但是,我正在尝试使WordPress插件中的实现选项尽可能干净,这就是我目前遇到的问题。

This JSFiddle is working and shows what I like to get as output: http://jsfiddle.net/golabs/tpKU3/ 该JSFiddle正在运行,并显示了我希望得到的输出: http : //jsfiddle.net/golabs/tpKU3/

But I would like to be able to create a new chart by just calling a function and supply the target div ID and the 2 dataArray's. 但是我希望能够仅通过调用一个函数并提供目标div ID和2个dataArray来创建新图表。 My attempt can be found here: http://jsfiddle.net/golabs/rstpA/ 我的尝试可以在这里找到: http : //jsfiddle.net/golabs/rstpA/

or see the jscode here: 或在这里查看jscode:

/**
 * However this second section of code does NOT work as nothing is displayed in the container21 div...
 * What am I doing wrong here?
 * I like to be able to create a new chart by only supplying the target div ID and the dataArray's, this
 * to have be able to manage the general settings centrally.
 *
 * This JSFIDDLE is related to: http://jsfiddle.net/golabs/tpKU3/
**/
$(function () {
  var __catergories__ = ['AAA','BBB','CCC','DDD','EEE','FFF','GGG'];

  var getChartConfig = function( renderId, dataArray1, dataArray2 ) {
    var config = {};
    config.chart = {
      renderTo: renderId, polar: true, type: 'line', height: 254, width: 360, className: 'SpiderPolarChart', backgroundColor: 'rgba(101,101,101,.1)'
    };
    config.title = {
      text: ''       
    };
    config.legend = {
      layout: 'horizontal', align: 'center', verticalAlign: 'bottom', y: -3,
    };
    config.credits = {
      enabled: true, text: 'highcharts.com', href: 'http://highcharts.com/', position: {
        verticalAlign: 'bottom',
        x: -11,
      }
    };
    config.pane = {
      startAngle: -90, endAngle: 90, center: ['50%', '93%'], size: 300, background: null
        };
    config.xAxis = {
      categories: __catergories__,
      tickmarkPlacement: 'on', tickPixelInterval: 1, lineWidth: 0, labels: {
        rotation: 'auto',
        distance: 12
      }, showLastLabel: true   
    };
    config.yAxis = {
      gridLineInterpolation: 'circle', gridLineDashStyle: 'LongDash', tickInterval: 1, lineWidth: 0, min: 0, max: 5, ceiling: 5, labels: {
        x: 4, y: 15
      }, showLastLabel: true
    };
    config.exporting = { 
      enabled: false
    };
    config.tooltip = {
      shared: true, pointFormat: '<span style="color:{series.color}">{series.name}: <b>{point.y:,.0f}</b><br/>'
    };
    config.series = [{
      name: 'Overall Rating', data: dataArray1, color: '#D4A539', type: 'area', pointPlacement: 'on', connectEnds: false, index: 1, legendIndex: 2
      },{
      name: 'Personal Rating', data: dataArray2, color: '#333', type: 'line', pointPlacement: 'on', connectEnds: false, index: 2, legendIndex: 1            
    }];
    return config;
    };

  // ===========================================================================
  charts.push(new Highcharts.Chart(
    getChartConfig( "container21", [1.5,3,2,3,4,1,2], [1,4,1.5,3,2,3,1] )
  ));
});

I've run the code through JSHint.com so all typos should be tackled as it doesn't show any errors at the moment. 我已经通过JSHint.com运行了代码,因此应该解决所有错别字,因为它目前不显示任何错误。

I hope someone can guide me into the right direction to get this to work. 我希望有人可以引导我朝着正确的方向发展。

Cheers! 干杯!

** Updated the links to the JSFiddles ** ** ISSUE RESOLVED, see below ** **更新了指向JSFiddles的链接** **已解决问题,请参见下文**

In this jsFiddle ( http://jsfiddle.net/golabs/rstpA/ ) you are not telling it what chart is. 在此jsFiddle( http://jsfiddle.net/golabs/rstpA/ )中,您没有告诉它什么chart You are trying to set the options for a chart that you have not created to update the options you are passing. 您正在尝试为尚未创建的图表设置选项,以更新正在传递的选项。 It is sort of backwards. 这有点倒退。

Here is a quick "fix": 这是一个快速的“修复”:

  var newChart = new Highcharts.Chart(
    getChartConfig( "container21", [1.5,3,2,3,4,1,2], [1,4,1.5,3,2,3,1] )
  );

Now, I would think it would be better to setup a global option set and then merge in your new set (specific to chart you want to build) as in my linked SO answer. 现在,我认为最好设置一个全局选项集,然后合并到新的集合中(特定于要构建的图表),就像在链接的SO答案中那样。 This may or may not give you more flexibility. 这可能会或可能不会给您带来更大的灵活性。

charts was not defined. 图表未定义。 Is this the solution you are looking for? 这是您正在寻找的解决方案吗?

var charts = [];

charts.push(new Highcharts.Chart(

getChartConfig( "container21", [1.5,3,2,3,4,1,2], [1,4,1.5,3,2,3,1] )

));

http://jsfiddle.net/37Gsv/1/ http://jsfiddle.net/37Gsv/1/

you may try this: 您可以尝试以下方法:

var charts = []; 
  var i=0;
    for (i =1; i< 10; i++){
    var o =i.toString();
    charts.push(new Highcharts.Chart(
    getChartConfig( "container2"+o, [1.5,3,2,3,4,1,2], [1,4,1.5,3,2,3,1] )
  ));
  }

test link: http://jsfiddle.net/newpiseth/8u0tkkkL/5/ 测试链接: http : //jsfiddle.net/newpiseth/8u0tkkkL/5/

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

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