ChartJS——基本样式

单双曲线
canvas

<script src=""></script>

<div style="width:400px; margin:0px auto">
      <canvas id="myChart"></canvas>
</div>

<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx,{
      type:'line',
      data:{
            labels:['红', '黄', '蓝', '绿', '紫', '橙'],
            datasets:[
            {
                  label:'示例',
                  data:[12, 19, 3, 5, 0, 3],
                  borderColor:'blue',
                  backgroundColor:'skyBlue',
                  borderWidth:1,
                  fill:false,
            },
            {//双曲线就加一个如出一辙的datasets就好了
                  label:'示例2',
                  data:[120, 190, 30, 50, 0, 30],
                  borderColor:'red',
                  backgroundColor:'pink',
                  borderWidth:1,
                  fill:false,
            }
            ]
      }
});
</script>

多轴
3d

代码同上
在 fill:false, 下分别增长一行:code

yAxisID:'y-axis-1'

yAxisID:'y-axis-2'

虚线
blog

同上
data:{},
options:{
      scales:{
            yAxes:[{
                  type:'linear',
                  display:true,
                  position:'left',
                  id:'y-axis-1',
            },{
                  type:'linear',
                  display:true,
                  position:'right',
                  id:'y-axis-2',
                  gridLines:{drawOnChartArea:false}
            }
            }],
      }
}

柱状图
垂直
ip

type:'bar',

水平
get

type:'horizontalBar',

多数据
it

datasets:[
      {},
      {}
]

多轴
io

<div style="width:400px;margin:0px auto">
    <canvas id="myChart" ></canvas>
</div>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['红', '蓝', '黄', '绿', '紫', '橙'],
        datasets: [
       {
            label: '示例1',
            data: [12, 19, 3, 5, 0, 3],
            borderColor:'blue',
            backgroundColor:'skyBlue',
            borderWidth: 1,
            yAxisID: 'y-axis-1',
        },
       {
            label: '示例2',
            data: [182, 51, 133, 54, 105, 96],
            borderColor:'red',
            backgroundColor:'pink',
            borderWidth: 1,
            yAxisID: 'y-axis-2',
        },
 
]
    },
    options:{
        scales:{
                        yAxes: [{
                            type: 'linear',
                            display: true,
                            position: 'left',
                            id: 'y-axis-1',
                        }, {
                            type: 'linear',
                            display: true,
                            position: 'right',
                            id: 'y-axis-2',
                            gridLines: {
                                drawOnChartArea: false
                            }
                        }],       
 
        }
    }
});
</script>

饼状图
grid

<div style="">
      <canvas id="myChart"></canvas>
</div>

<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx,{
      type:'pie',
      data:{
            label:['红','蓝','黄','绿','紫','橙'],
            data:[12, 19, 3, 5, 0, 3],
            borderColor:'lightGray',
            backgroundColor:['pink', 'skyblue', 'LightYellow', 'LawnGreen','MediumPurple', 'orange'],
            borderWidth:1
      }
});
</script>

甜甜圈
im

同饼状图
type:'doughnut'
相关文章
相关标签/搜索