如何在 chartist.js 中使用负数

How to use negative numbers in chartist.js

我想在 chartist.js 中显示负数,但它似乎不支持负数 :

我的代码:

        var _4date = 'D';
        var _3date = 'C';
        var _2date = 'B';
        var _1date = 'A';

        var _4all_scores = '-100';
        var _3all_scores = '476';
        var _2all_scores = '649';
        var _1all_scores = '0';

        var _4comments_scores = '-100';
        var _3comments_scores = '20';
        var _2comments_scores = '0';
        var _1comments_scores = '0';


        var _4transactions_scores = '0';
        var _3transactions_scores = '456';
        var _2transactions_scores = '649';
        var _1transactions_scores = '0';



        var chart = new Chartist.Line('.ct-chart', {
            labels: [_1date, _2date, _3date, _4date],
            series: [
                [_1all_scores, _2all_scores, _3all_scores, _4all_scores],
                [_1comments_scores, _2comments_scores, _3comments_scores, _4comments_scores],
                [_1transactions_scores, _2transactions_scores, _3transactions_scores, _4transactions_scores]
            ]
        }, {
            low: 0,
            showArea: true,
            showPoint: false,
            fullWidth: true
        });

        chart.on('draw', function(data) {
            if(data.type === 'line' || data.type === 'area') {
                data.element.animate({
                    d: {
                        begin: 2000 * data.index,
                        dur: 2000,
                        from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
                        to: data.path.clone().stringify(),
                        easing: Chartist.Svg.Easing.easeOutQuint
                    }
                });
            }
        });

https://gionkunz.github.io/chartist-js/examples.html

已解决:

我刚刚删除了这个选项:

low: 0

也许这会对任何人有所帮助...

我有一个面积图,其中只有负数,定义了

var low = Math.min(...data) * 0.75;
var high = Math.max(...data) * 1.25;

这只适用于图表中的正数,这导致图表被裁剪,我根本没有看到任何图表。所以对于只有负数的图表,我定义了这个:

var low = Math.max(...data) * -1.25;
var high = Math.min(...data) * -0.75;

我这样做是因为我用 value * -1 恢复了我的图表 - 那是因为镜像图表的内置功能对我不起作用。

所以,我得到了这样的结果:

    function chartOptions(data, reversed=false) {

        if(reversed) {
            var low = Math.max(...data) * -1.25;
            var high = Math.min(...data) * -0.75;
        } else {
            var low = Math.min(...data) * 0.75;
            var high = Math.max(...data) * 1.25;
        }

        return {
            showPoint: true,
            showLine: true,
            showArea: true,
            fullWidth: true,
            showLabel: false,
            low: low,
            high: high
        }
    }