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'
相關文章
相關標籤/搜索