将常规amChart设置存储为变量并在函数内重复使用

时间:2016-04-18 11:35:55

标签: javascript

我有很多功能可以为我填充amChart。很多amChart设置在不同的功能上保持不变。我的一个职能是:

function updateIndicatorsAllScores() {

    organization = $("#organization").val();
    indicator = $("#indicators").val();
    funcid = "fill_chart_all_scores";

    //console.log('changed'); 
    $.getJSON('functions/getfunctions.php', {
        "organization":organization,
        "indicator":indicator,
        "funcid":funcid},

    function(dataChart) {

            var chart = AmCharts.makeChart("chartallscores", {
            "theme": "light",
            "type": "serial",
            "startDuration": 1,
            "dataProvider": dataChart,
            "rotate": false,
            "categoryField": "organisatie",
              "valueAxes": [ {
                "gridColor": "#FFFFF",
                "gridAlpha": 0.2,
                "dashLength": 0
              } ],
              "gridAboveGraphs": true,
              "startDuration": 1,
              "graphs": [ {
                "balloonText": "[[category]]: <b>[[value]]</b>",
                "fillColorsField": "fillcolor", //Dit veld heb ik meegegeven vanuit SQL functie en bevat de HEX kleurcodes BD
                "fillAlphas": 0.8,
                "lineAlpha": 0.2,
                "type": "column",
                "valueField": "score"
              } ],
              "chartCursor": {
                "categoryBalloonEnabled": false,
                "cursorAlpha": 0,
                "zoomable": false
              },
              "categoryAxis": {
                "gridPosition": "start",
                "gridAlpha": 0,
                "tickPosition": "start",
                "tickLength": 20
              },
              "export": {
                "enabled": true
              }

        },0);

    $('.chart-input').off().on('input change',function() {
        var property = jQuery(this).data('property');
        var target = chart;
        chart.startDuration = 0;

        });
    });
}

我想要的是将var = AmCharts.makechart与所有图表属性存储一次,并且在函数内仅更改"dataPRovider: dataChart,

1 个答案:

答案 0 :(得分:1)

var dataProvider2 = [ {
    "country": "USA",
    "visits": 1000
  }, {
    "country": "China",
    "visits": 1000
  }, {
    "country": "Japan",
    "visits": 1200
  }, {
    "country": "Germany",
    "visits": 2000
  }, {
    "country": "UK",
    "visits": 1000
  }, {
    "country": "France",
    "visits": 1000
  }, {
    "country": "India",
    "visits": 984
  }, {
    "country": "Spain",
    "visits": 711
  }, {
    "country": "Netherlands",
    "visits": 500
  }, {
    "country": "Russia",
    "visits": 500
  }, {
    "country": "South Korea",
    "visits": 443
  }, {
    "country": "Canada",
    "visits": 441
  }, {
    "country": "Brazil",
    "visits": 395
  } ];

var dataProvider1 = [ {
    "country": "USA",
    "visits": 2025
  }, {
    "country": "China",
    "visits": 1882
  }, {
    "country": "Japan",
    "visits": 1809
  }, {
    "country": "Germany",
    "visits": 1322
  }, {
    "country": "UK",
    "visits": 1122
  }, {
    "country": "France",
    "visits": 1114
  }, {
    "country": "India",
    "visits": 984
  }, {
    "country": "Spain",
    "visits": 711
  }, {
    "country": "Netherlands",
    "visits": 665
  }, {
    "country": "Russia",
    "visits": 580
  }, {
    "country": "South Korea",
    "visits": 443
  }, {
    "country": "Canada",
    "visits": 441
  }, {
    "country": "Brazil",
    "visits": 395
  } ];

  var chtrvals = {
  "type": "serial",
  "theme": "light",
  "dataProvider": dataProvider1,
  "valueAxes": [ {
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  } ],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [ {
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "visits"
  } ],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "country",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0,
    "tickPosition": "start",
    "tickLength": 20
  },
  "export": {
    "enabled": true
  }

};
var chart = AmCharts.makeChart( "chartdiv", chtrvals );

function changedp(){
       chart.dataProvider =  dataProvider2;
       chart.validateData();
}