Chart.js以定义的值启动Y轴

时间:2022-12-04 10:07:45

A Plugin that I use on my website use Chart.js I try to start Y-Axis at a defined value.

我在我的网站上使用的插件使用Chart.js我尝试以定义的值启动Y轴。

Here's a picture of what I have now : Line Chart Chart.js以定义的值启动Y轴

这是我现在所拥有的图片:折线图

the javascript code :

javascript代码:

(function() {
Chart.types.Line.extend({
    name:'UncontinuousLine',
    defaults:{scaleBeginAtZero:false},
    initialize:function(data) {
        Chart.types.Line.prototype.initialize.apply(this,arguments);
    },
    draw:function() {
        Chart.types.Line.prototype.draw.apply(this,arguments);
        var ctx=this.chart.ctx;
        this.datasets.forEach(function(ds) {
            ctx.strokeStyle=ds.strokeColor;
            var prevPt={value:2.49};
            ds.points.forEach(function(curPt) {
                if(parseInt(curPt.value)<=0) {
                    curPt.value=prevPt.value;
                } 
                if(parseInt(curPt.value)>0&&parseInt(prevPt.value)>0) {
                    ctx.beginPath();
                    ctx.moveTo(prevPt.x,prevPt.y);
                    ctx.lineTo(curPt.x,curPt.y);
                    ctx.stroke();
                } 
                prevPt=curPt;
            });
        });

    }
});})(); 

and the php code :

和PHP代码:

private function _create_uncontinuousline_chart( $data, $opt ){ 
    if ( empty( $data ) ){ 
        return '';
    } 
    $id = self::_get_canvas_id( $this->count );
    $sets = self::_parse_data( $data, 3 );
    $cd = self::_resort_sets( $sets, true );
    $this->js[] = 'new Chart(' . "jQuery('#$id').get(0).getContext('2d')" . ')
                    .UncontinuousLine(' . AimyChartsContentPluginHelper::phpva_json_encode( $cd ) . ',' 
                    . '{' . 'animation: ' . ( $opt[ 'animate' ] ? 'true' : 'false' ) . ',' 
                    . 'multiTooltipTemplate: ' . '"<%= value %> (<%= datasetLabel %>)"' 
                    . ',responsive: ' . ( $opt[ 'responsive' ] ? 'true' : 'false' ) 
                    . ',datasetStrokeWidth:0.01' . '}' . ');';
    return self::_get_canvas( 'UncontinuousLine', $this->count, $opt );
}

I've tried many things but impossible to start my y-axis at 1 for example... Does someone know how i can do that ?

我已经尝试了很多东西但是不可能以1开始我的y轴...有人知道我怎么能这样做吗?

1 个解决方案

#1


0  

In your options, use this:

在您的选项中,使用此:

 scaleBeginAtZero : false,
 scaleOverride: true,
 scaleStartValue: 1,

Try this for your php code:

试试这个PHP代码:

private function _create_uncontinuousline_chart( $data, $opt ){ 
    if ( empty( $data ) ){ 
        return '';
    } 
    $id = self::_get_canvas_id( $this->count );
    $sets = self::_parse_data( $data, 3 );
    $cd = self::_resort_sets( $sets, true );
    $this->js[] = 'new Chart(' . "jQuery('#$id').get(0).getContext('2d')" . ')
                    .UncontinuousLine(' . AimyChartsContentPluginHelper::phpva_json_encode( $cd ) . ',' 
                    . '{' . 'animation: ' . ( $opt[ 'animate' ] ? 'true' : 'false' )  
                    . ',multiTooltipTemplate: ' . '"<%= value %> (<%= datasetLabel %>)"' 
                    . ',responsive: ' . ( $opt[ 'responsive' ] ? 'true' : 'false' ) 
                    . ',datasetStrokeWidth:0.01' 
                    . ',scaleBeginAtZero : false'
                    . ',scaleOverride: true'
                    . ',scaleStartValue: 1'
                    . '}' . ');';
    return self::_get_canvas( 'UncontinuousLine', $this->count, $opt );

#1


0  

In your options, use this:

在您的选项中,使用此:

 scaleBeginAtZero : false,
 scaleOverride: true,
 scaleStartValue: 1,

Try this for your php code:

试试这个PHP代码:

private function _create_uncontinuousline_chart( $data, $opt ){ 
    if ( empty( $data ) ){ 
        return '';
    } 
    $id = self::_get_canvas_id( $this->count );
    $sets = self::_parse_data( $data, 3 );
    $cd = self::_resort_sets( $sets, true );
    $this->js[] = 'new Chart(' . "jQuery('#$id').get(0).getContext('2d')" . ')
                    .UncontinuousLine(' . AimyChartsContentPluginHelper::phpva_json_encode( $cd ) . ',' 
                    . '{' . 'animation: ' . ( $opt[ 'animate' ] ? 'true' : 'false' )  
                    . ',multiTooltipTemplate: ' . '"<%= value %> (<%= datasetLabel %>)"' 
                    . ',responsive: ' . ( $opt[ 'responsive' ] ? 'true' : 'false' ) 
                    . ',datasetStrokeWidth:0.01' 
                    . ',scaleBeginAtZero : false'
                    . ',scaleOverride: true'
                    . ',scaleStartValue: 1'
                    . '}' . ');';
    return self::_get_canvas( 'UncontinuousLine', $this->count, $opt );