Highcharts:从CSV加载数据时默认禁用系列

时间:2015-01-07 16:17:38

标签: javascript jquery csv highcharts

我有一堆由CSV文件制作的图表,我希望默认情况下禁用某些系列。该系列是使用highcharts数据处理功能制作的,我不知道如何定制它们。我试过了:

switch (plotOptions.series.name) { case "DACH - Austria, Germany and Switzerland": $(this.hide()); break; }

但它不会奏效。关于如何做到这一点的任何想法?我想写一个开关,因为默认情况下应该禁用8/13系列...小提琴下面



            $("select").change(function() {
                $("select option:selected").each(function() {

                  var variable = $(this).val();

                  var graphtitle;
                  switch (variable) {
                    case "CSV/Sheet1.csv":
                      graphtitle = "Academic-Corporate Collaboration";
                      break;
                    case "CSV/Sheet2.csv":
                      graphtitle = "Academic-Corporate Collaboration Impact";
                      break;
                    case "CSV/Sheet3.csv":
                      graphtitle = "Citation Count";
                      break;
                    case "CSV/Sheet4.csv":
                      graphtitle = "Citation Count, self-citations excluded";
                      break;
                    case "CSV/Sheet5.csv":
                      graphtitle = "Citations per Publication";
                      break;
                    case "CSV/Sheet6.csv":
                      graphtitle = "Citations per publication, self-citations excluded";
                      break;
                    case "CSV/Sheet7.csv":
                      graphtitle = "Cited publications (%)";
                      break;
                    case "CSV/Sheet8.csv":
                      graphtitle = "Cited publications (%), self-citations excluded";
                      break;
                    case "CSV/Sheet9.csv":
                      graphtitle = "Field-Weighted Citation Impact";
                      break;
                    case "CSV/Sheet10.csv":
                      graphtitle = "Output in top 10 percentiles (%)";
                      break;
                    case "CSV/Sheet11.csv":
                      graphtitle = "Output in top 10 percentiles (%), self-citations excluded";
                      break;
                    case "CSV/Sheet12.csv":
                      graphtitle = "Publications in top 10 journal percentiles (%, SJR)";
                      break;
                    case "CSV/Sheet13.csv":
                      graphtitle = "Scholarly Output";
                      break;
                  }

                  $.get(variable, function(csv) {
                    $('.graphcontainer').highcharts({
                      chart: {
                        type: 'line'
                      },
                      data: {
                        csv: csv,
                        itemDelimiter: ';'
                      },
                      title: {
                        text: graphtitle
                      },
                      plotOptions: {
                        series: {
                          connectNulls: true
                        }
                      },
                      yAxis: {
                        title: {
                          text: ''
                        }
                      },
                      legend: {
                        layout: 'vertical'
                      },
                      credits: {
                        enabled: false
                      },
                      tooltip: {
                        formatter: function() {
                          var s = [];
                          $.each(this.points, function(i, point) {
                            s.push('<span class="tooltip">' + point.series.name + ' : ' +
                              point.y + '<br><span>');
                          });
                          return s.join('');
                        },
                        shared: true
                      }
                    });
                  });
                  switch (plotOptions.series.name) {
                    case "DACH - Austria, Germany and Switzerland":
                      $(this.hide());
                      break;
                  }

                });
              })
              .change();
&#13;
.graphcontainer {
  width: 80%;
  height: 600px;
  margin: auto;
  border: 1px solid black;
}
#selectcontainer {
  width: 80%;
  margin: auto;
  border: 1px solid black;
}
#CSVinput {
  width: 60%;
  font-family: verdana;
  margin-left: 20%;
}
&#13;
<!DOCTYPE html>

<html>

<head>
  <title>Grafi IJS</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link type="text/css" rel="stylesheet" href="grafi.css" />

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
  <script type="text/javascript" src="http://code.highcharts.com/modules/data.js"></script>

</head>

<body>
  <div class="graphcontainer"></div>
  <div id="selectcontainer">
    <select id="CSVinput">
      <option value="CSV/Sheet1.csv" selected="selected">Academic-Corporate Collaboration</option>
      <option value="CSV/Sheet2.csv">Academic-Corporate Collaboration Impact</option>
      <option value="CSV/Sheet3.csv">Citation Count</option>
      <option value="CSV/Sheet4.csv">Citation Count, self-citations excluded</option>
      <option value="CSV/Sheet5.csv">Citations per Publication</option>
      <option value="CSV/Sheet6.csv">Citations per publication, self-citations excluded</option>
      <option value="CSV/Sheet7.csv">Cited publications (%)</option>
      <option value="CSV/Sheet8.csv">Cited publications (%), self-citations excluded</option>
      <option value="CSV/Sheet9.csv">Field-Weighted Citation Impact</option>
      <option value="CSV/Sheet10.csv">Output in top 10 percentiles (%)</option>
      <option value="CSV/Sheet11.csv">Output in top 10 percentiles (%), self-citations excluded</option>
      <option value="CSV/Sheet12.csv">Publications in top 10 journal percentiles (%, SJR)</option>
      <option value="CSV/Sheet13.csv">Scholarly Output</option>
    </select>
  </div>

  <script type="text/javascript" src="grafi.js"></script>

</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以使用chart.events.load执行此操作:

chart: {
    events: {
        load: function () {
            var theSeries = this.series;
            $.each(theSeries, function () {
                if (this.index > 0) {
                    this.setVisible(false);
                }
            });
        }
    }
}

此示例仅检查系列的索引是否大于0并将可见性设置为false,以便只有第一个系列在图表上可见。您可以在其中添加其他支票以满足您的需要(例如this.name)。