Search code examples
jqueryamcharts

amcharts logarithmic scale toggling


I have data that varies, meaning that some appears more readable with linear scales, while other requires logarithmic scale.

I'm trying to draw a stacked area graph, via the following code

AmCharts.makeChart("amchart-metrics-history-"+tag_id, {
                        "type": "serial",
                        "theme": "dark",
                        "titles": [{
                                "text": "Tag Metrics",
                                "size": 15
                        }],
                        "pathToImages": "/static/img/amcharts/",
                        "legend": {
                            "equalWidths": true,
                            "periodValueText": "",
                            "position": "bottom",
                            "valueAlign": "left",
                            "valueWidth": 100
                        },
                        "dataProvider": data.data,
                        "dataDateFormat": "YYYY-MM-DD HH:NN:SS",
                        "graphs": [{
                            "fillAlphas": 0.6,
                            "lineAlpha": 0.4,
                            "title": "Malware",
                            "valueField": "malware_count",
                            "color": "#fff"
                        }, {
                            "fillAlphas": 0.6,
                            "lineAlpha": 0.4,
                            "title": "Benign",
                            "valueField": "benign_count"
                        }, {
                            "fillAlphas": 0.6,
                            "lineAlpha": 0.4,
                            "title": "Grayware",
                            "valueField": "grayware_count"
                        }],
                        "valueAxes": [{
                            "stackType": "regular",
                            "title": "Sample Count"
                        }],
                        "plotAreaBorderAlpha": 0,
                        "marginTop": 10,
                        "marginLeft": 0,
                        "marginRight":30,
                        "marginBottom": 0,
                        "chartScrollbar": {},
                        "chartCursor": {
                            "cursorAlpha": 0
                        },

                        // X-Axis values
                        "categoryField": "time",
                        "categoryAxis": {
                            "parseDates": true,
                            //"startOnAxis": true,
                            //"axisColor": "#DADADA",
                            //"gridAlpha": 0.07,
                            "title": "Date",
                            "minPeriod": "mm",
                        },
                        "export": {
                            "enabled": false
                         },

                        "zoomControl": {
                            "zoomControlEnabled": true,
                        },

                        // Y-Axis specific
                        "valueAxes": [{
                            "logarithmic": true,
                        }]
                    });

What I'm trying to accomplish is a redraw once I toggle the Y scale. I've been trying to use the following

$("#scale_change_"+tag_id).change(function() {
                        if ( $(this).is(":checked") ) {
                            am_chart.valueAxes[0].logarithmic = true;
                        } else {
                            am_chart.valueAxes[0].logarithmic = false;
                        }
                        am_chart.validateData();
                    });

This doesn't work unfortunately. Can someone shed some light onto what I should be doing instead to toggle the scale, then force a redraw? Ideally, stacking should persist between scale changes...


Solution

  • I had the exact same problem. The logarithmic scale just doesn't work for all of my datasets. It works for some and not for others. I figured out that if you want an axis to have logarithmic scale, you have to make sure that:

    -The data of the axis does NOT have ZERO values

    -The data of the axis does NOT have NEGATIVE values

    It is probably because of the way the logarithmic functionality of this library is coded.