如何在chartist.js中使用负数

时间:2016-06-30 04:38:08

标签: javascript 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
                    }
                });
            }
        });

enter image description here

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

2 个答案:

答案 0 :(得分:1)

<强>解决:

我刚删除了这个选项:

low: 0

答案 1 :(得分:0)

也许这会帮助任何人...

我有一个仅定义了负数的面积图

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, hight: high } } 还原了图表-这是因为用于镜像图表的内置函数对我没有作用。

所以,我最终得到了这样的东西:

SELECT *, Multiplier =
    Case PRODUCTNAME 
    When  'Voice' then 2.4
    When  'Cable' then 4.8
    Else 1
    END,
    Multiplier * COMP_RATE as Commission
  into #CommissionsFTMJul18
  FROM [DBPO_TRANS].[dbo].[CREDITS]