AmChart-更新fieldMappings和dataSet后,Graph没有绘制

时间:2017-03-29 07:19:50

标签: amcharts

问题陈述:

在运行期间(点击一个按钮之后)更新stockMackings和dataSet之后,validateNow()/ validteData()没有绘制图表。

注意:MACD0从第25个元素开始添加,expoSignalLine0从数据提供者中的第33个元素添加, fieldMapping 也会更新,并且可以在控制台中进行相同的验证。enter code here

以下是代码段: (单击按钮时调用addMACD函数)

function addMACD() {
    var chart = AmCharts.charts[ 0 ];
    AmCharts.MACDGraphs = 0;
    AmCharts.expoSignalLineGraphs = 0;
    var MACDField = "MACD"+ AmCharts.MACDGraphs;
    var expoSignalLineField = "expoSignalLine"+ AmCharts.expoSignalLineGraphs;

    chart.dataSets[0].fieldMappings.push( {
            fromField : MACDField,
            toField : MACDField
    },

    {
            fromField : expoSignalLineField,
            toField : expoSignalLineField
    });

     var currClose;
     var prevClose;
     var twelveDayEMA =[];
     var twentySixDayEMA =[];
     var MACDarray = [];
     var signalLineArray = [];
     var MACDperiod = 9 ;// 9 day exponential average
    for ( var i = 1; i < (chart.dataSets[0].dataProvider.length); i++) {

            var dp = chart.dataSets[0].dataProvider[i - 1];
            prevClose = parseFloat(dp["close"]);

            var dp = chart.dataSets[0].dataProvider[i];
            currClose = parseFloat(dp["close"]);
            if( i==1){
                    twelveDayEMA[i] = (0.15*currClose) + (0.85*prevClose);
                    twentySixDayEMA[i] = (0.075*currClose) + (0.925*prevClose);
            }
            else{
                    twelveDayEMA[i] = (0.15*currClose) + (0.85*twelveDayEMA[i - 1]);
                    twentySixDayEMA[i] = (0.075*currClose) + (0.925*twentySixDayEMA[i - 1]);
            }

            if(i >= 25){

                    MACDarray[i] = twelveDayEMA[i] - twentySixDayEMA[i] ;
                    dp[MACDField] = MACDarray[i];

                    if(i == 25){
                            signalLineArray[i] =  MACDarray[i];

                    }
                    else{
                            signalLineArray[i] = ( MACDarray[i]*(2/( MACDperiod + 1)) ) + ( signalLineArray[i - 1]*(1-(2/( MACDperiod + 1))) )        
                    }
            }
            if(i >=33){
                    dp[expoSignalLineField] = signalLineArray[i];
            }

    }
    console.log(chart);


  if ( chart.panels.length == 1 || chart.panels.length == 2 || chart.panels.length == 3 || chart.panels.length == 4 || chart.panels.length == 5) {

var newPanel = new AmCharts.StockPanel();
newPanel.allowTurningOff = true;
newPanel.title = "MACD";
newPanel.showCategoryAxis = false;

graph1 = new AmCharts.StockGraph();
graph1.valueField = MACDField;
graph1.useDataSetColors = false;
graph1.lineColor="#6699FF";
graph1.title = "MACD";
newPanel.stockGraphs.push( graph1 );

graph2 = new AmCharts.StockGraph();
graph2.valueField =expoSignalLineField;
graph2.useDataSetColors = false;
graph2.lineColor = "#990000";
graph2.title = "MACD2";
newPanel.stockGraphs.push( graph2 );

var legend = new AmCharts.StockLegend();
legend.markerType = "none";
legend.markerSize = 0;
newPanel.stockLegend = legend;
chart.addPanelAt( newPanel, 1 );



 chart.validateData();
 chart.validateNow();
 //chart.write("chartdiv");
  }
 }

1 个答案:

答案 0 :(得分:0)

您必须先致电validateNow,然后致电validateData

或者,您可以调用validateNow(true, false),其效果与分别调用这两个函数相同。

Updated fiddle