saf
saf

Reputation: 259

Issues with amcharts

I am making stock charts with amcharts library . Here is what I have achieved enter image description here

And here is what I want enter image description here

You can compare both picture and see I have cylindrical and moving avg graph (dashed lines) but I am not able to add spikes which are showing at the bottom of graph in original one. (Pink and orange color). I don't know how to add those spikes in stock graph . My data is dynamic so I can't create a js fiddle but I can provide my function here-

function load_stock_graph(graph_type, history, title, aggregate){
    // DEFINE CHART PLUGINS
    AmCharts.averageGraphs = 0;
    AmCharts.addMovingAverage = function (dataSet, panel, field, aggregate, col ,graph) {
        // update dataset
        var avgField = "avg"+AmCharts.averageGraphs;
        dataSet.fieldMappings.push({
            fromField: avgField,
            toField: avgField});

        // calculate moving average
        var fc = 0;
        var sum = 0;
        for (var i = 0; i < dataSet.dataProvider.length; i++) {
           // console.log(aggregate[i].ema_0008)
            var dp = dataSet.dataProvider[i];
            if (dp[field] !== undefined) {
                sum += dp[field];
                fc++;
                if (aggregate[i] !== undefined){
                    if(col == '08'){
                        dp[avgField] = aggregate[i].ema_0008;
                    }else if(col == '15'){
                        dp[avgField] = aggregate[i].ema_0015;
                    }else if(col == '20'){
                        dp[avgField] = aggregate[i].ema_0020;
                    }
                }
            }
        }

        // create a graph
        graph.valueField = avgField;
        panel.stockGraphs.push(graph);

        // increment average graph count
        AmCharts.averageGraphs++;
    }

    // CHART DATA
    var chartData = [];
        var chartData1 = [];

    generateChartData(history);

    function generateChartData(history) {
        var firstDate = new Date();
        firstDate.setHours(0, 0, 0, 0);
        firstDate.setDate(firstDate.getDate() - 2000);

        for (var i = 0; i < history.length; i++) {
            var date = new Date(history[i].date);

             var val = Math.round(Math.random() * (30) + 100);

            chartData[i] = ({
                date: date,
                open: history[i].open,
                close: history[i].close,
                high: history[i].high,
                low: history[i].low,
                volume: history[i].volume,
                value: val
            });



        }
    }



    // CHART CONFIG
    var chartConfig =  {
        type: "stock",
        pathToImages : "/static/img/amcharts/",

        dataSets: [{
            fieldMappings: [{
                fromField: "open",
                toField: "open"
            }, {
                fromField: "close",
                toField: "close"
            }, {
                fromField: "high",
                toField: "high"
            }, {
                fromField: "low",
                toField: "low"
            }, {
                fromField: "volume",
                toField: "volume"
            }, {
                fromField: "value",
                toField: "val"
            }],

            color: "#fff",
            dataProvider: chartData,
            title: title,
            categoryField: "date",
            compared: false,
        }, 
        {
            fieldMappings: [{
                fromField: "value",
                toField: "value"
            }],
            color: "#fff",
            dataProvider: chartData,
            title: title,
            categoryField: "date"
        },


        ],


        panels: [{
                title: "Value",
                percentHeight: 70,


                stockGraphs: [{type: graph_type,
                      id: "g1",
                      openField: "open",
                      closeField: "close",
                      highField: "high",
                      lowField: "low",
                      valueField: "close",
                      lineColor: "#fff",
                      fillColors: "#fff",
                      negativeLineColor: "#db4c3c",
                      negativeFillColors: "#db4c3c",
                      fillAlphas: 1,
                      comparedGraphLineThickness: 2,
                      columnWidth: 0.7,
                      useDataSetColors: false,
                      comparable: true,
                      compareField: "close",
                      showBalloon: false,
                      //proCandlesticks: true
                    } ],

                stockLegend: {
                    valueTextRegular: undefined,
                    periodValueTextComparing: "[[percents.value.close]]%"
                }
            },

            {
                title: "Volume",
                percentHeight: 40,
                marginTop: 1,
                columnWidth: 0.6,
                showCategoryAxis: false,
                valueAxes: [{
                    usePrefixes: true
                }],



                stockGraphs: [ {
                      valueField: "volume",
                      openField: "open",
                      type: "column",
                      showBalloon: false,
                      fillAlphas: 1,
                      lineColor: "#fff",
                      fillColors: "#fff",
                      negativeLineColor: "#db4c3c",
                      negativeFillColors: "#db4c3c",
                      useDataSetColors: false,
                    } ],

                stockLegend: {
                    markerType: "none",
                    markerSize: 0,
                    labelText: "",
                    periodValueTextRegular: "[[value.close]]"
                }
            }


        ],

        panelsSettings: {
          color: "#fff",
          plotAreaFillColors: "#333",
          plotAreaFillAlphas: 1,
          marginLeft: 60,
          marginTop: 5,
          marginBottom: 5
        },

        chartScrollbarSettings: {
            graph: "g1",
            graphType: "line",
            usePeriod: "WW",
            backgroundColor: "#333",
            graphFillColor: "#666",
            graphFillAlpha: 0.5,
            gridColor: "#555",
            gridAlpha: 1,
            selectedBackgroundColor: "#444",
            selectedGraphFillAlpha: 1
        },
        categoryAxesSettings: {
          equalSpacing: true,
          gridColor: "#555",
          gridAlpha: 1
        },

        valueAxesSettings: {
          gridColor: "#555",
          gridAlpha: 1,
          inside: false,
          showLastLabel: true
        },

        chartCursorSettings: {
            pan: true,
            valueLineEnabled:true,
            valueLineBalloonEnabled:true
        },

        legendSettings: {
          color: "#fff"
        },

        stockEventsSettings: {
          showAt: "high"
        },

        balloon: {
          textAlign: "left",
          offsetY: 10
        }, 

        periodSelector: {
            position: "bottom",

          periods: [ {
            period: "DD",
            count: 10,
            label: "10D"
          }, {
            period: "MM",
            count: 1,
            label: "1M"
          }, {
            period: "MM",
            count: 6,
            selected: true,
            label: "6M"
          }, {
            period: "YYYY",
            count: 1,
            label: "1Y"
          }, {
            period: "YYYY",
            count: 2,
            label: "2Y"
          }, {
            period: "YTD",
            label: "YTD"
          }, {
            period: "MAX",
            label: "MAX"
          } ]
        },
            "export": {
          "enabled": true,
          "backgroundColor": "#fff",
          "dataDateFormat": "YYYY-MM-DD"
        }
    }

    // ADD INDICATORS
    // ema_0008
    AmCharts.addMovingAverage(chartConfig.dataSets[0], chartConfig.panels[0], 'value',aggregate, '08', {
        useDataSetColors: false,
        color: "#ccffcc",
        lineColor: "#F4F009",
        title: "aggregate ema 0008",
        dashLength: 3
    });
    // ema_0015
    AmCharts.addMovingAverage(chartConfig.dataSets[0], chartConfig.panels[0], 'value',aggregate, '15',{
        useDataSetColors: false,
        color: "#ccffcc",
        lineColor: "#2C7F1D",
        title: "aggregate ema 0015",
        dashLength: 3
    });

    // ema_0020
    AmCharts.addMovingAverage(chartConfig.dataSets[0], chartConfig.panels[0], 'value',aggregate, '20',{
        useDataSetColors: false,
        color: "#ccffcc",
        lineColor: "#A109E8",
        title: "aggregate ema 0020",
        dashLength: 3
    });

    // Empty chart instance so that chart loaded via ajax can work correctly
    AmCharts.charts = [];

    // CREATE CHART
    var chart = AmCharts.makeChart("chartdiv", chartConfig);

}

Upvotes: 0

Views: 1425

Answers (1)

martynasma
martynasma

Reputation: 8595

Sounds like you just need to add another line graph in a separate panel.

Here's how to do it:

1) Add additional field in your data. You currently have open, high, low, close field. Let's add another one, say, "pink".

2) Update Data Set's fieldMappings property, to accommodate for the change:

   [{
      fromField: "open",
      toField: "open"
    }, {
      fromField: "close",
      toField: "close"
    }, {
      fromField: "high",
      toField: "high"
    }, {
      fromField: "low",
      toField: "low"
    }, {
      fromField: "volume",
      toField: "volume"
    }, {
      fromField: "value",
      toField: "val"
    }, {
      fromField: "pink",
      toField: "pink"
    }]

3) Add additional stock panel and a graph that will use the newly added data field:

  {
    percentHeight: 20,
    showCategoryAxis: false,
    stockGraphs: [{
      valueField: "pink",
      showBalloon: false,
      lineColor: "#fb02fe",
      lineThickness: 2,
      useDataSetColors: false
    }]
  }

If you would have wanted to add the indicator to some existing panel, you would just need to define the graph in it's stockGraphs array. I.e.:

  {
    percentHeight: 20,
    showCategoryAxis: false,
    stockGraphs: [{
      valueField: "pink",
      showBalloon: false,
      lineColor: "#fb02fe",
      lineThickness: 2,
      useDataSetColors: false
    }, {
      valueField: "red",
      showBalloon: false,
      lineColor: "#cc0000",
      lineThickness: 2,
      useDataSetColors: false
    }]
  }

Use this process to add as many indicators as you need.

Here's a working demo (with random values)

http://codepen.io/amcharts/pen/938b09efeabca4596c0a2cdaea60a269

Upvotes: 1

Related Questions