chartjs在vue2中的使用demo

<template>
  <div>
    <div style="display:inline-block">
      <canvas id="myChartBar" width="600" height="400"></canvas>
    </div>
    <div style="display:inline-block">
      <canvas id="myChartLine" width="600" height="400"></canvas>
    </div>
    <div>
      <canvas id="myChartPie" width="600" height="400"></canvas>
    </div>
  </div>
</template>

<script>
import Chart from 'chart.js/auto'
export default {
  name: 'ChartDemo',
  data () {
    return {}
  },
  methods: {
    initChartBar () {
      var ctx = document.getElementById("myChartBar");
      new Chart(ctx, {
          type: 'bar',
          data: {
              labels: ["1月","2月"],
              datasets: [
                {
                  label: 'one', // 系列名: 
                  data: [12, 19], // 数值
                  backgroundColor: 'red', // 背景颜色
                  hoverBackgroundColor: 'black', // 鼠标经过时背景颜色
                  borderWidth: 2, // 边框宽度
                  borderColor: 'black', // 边框颜色
                  hoverBorderColor: 'black', // 鼠标经过时边框颜色
                  borderAlign:'inner', // 边框对齐方式:'center'(default)|'inner'
              },
              {
                  label: 'two',// 系列名: 
                  data: [40, 60], // 数值
                  barThickness: 20, // 柱状的粗度
                  maxBarThickness: 30, // 柱状的最大粗度
                  backgroundColor: 'blue', // 背景颜色
                  hoverBackgroundColor: 'black', // 鼠标经过时背景颜色
                  borderWidth: 2, // 边框宽度
                  hoverBorderWidth: 10, // 鼠标经过时边框宽度
                  borderRadius: 10, // 边框圆角
                  hoverBorderRadius: 10, // 鼠标经过时边框圆角
                  borderColor: 'black', // 边框颜色
                  hoverBorderColor: 'black', // 鼠标经过时边框颜色
              },
            ]
          },
          options: {
            responsive: false, // 响应式
            plugins: {
              customCanvasBackgroundColor:{
                color: 'lightGreen'
              },
              title: {
                display: true, // 默认false
                align: 'start', // 默认是center,其它值有:start,end
                text: '这是title', // 文本
                color: 'red', // 字体颜色
                padding: 5, // 内边距,数字,或者写成{ bottom: number ; top: number }
                position: 'top', //默认top,其它值有: "left" | "top" | "bottom" | "right"
                fullSize: true, // 默认true, 此框应占用画布的全宽,如果是false, 方框放在图表区域的上方/旁边
                font: {
                  size: 45,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                }
              },
              subtitle: {
                display: true, // 默认false
                align: 'start', // 默认是center,其它值有:start,end
                text: '这是subtitle', // 文本
                color: '#999', // 字体颜色
                padding: 5, // 内边距,数字,或者写成{ bottom: number ; top: number }
                position: 'top', //默认top,其它值有: "left" | "top" | "bottom" | "right"
                fullSize: true, // 默认true, 此框应占用画布的全宽,如果是false, 方框放在图表区域的上方/旁边
                font: {
                  size: 30,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'normal', // 默认normal, italic, oblique, initial, inherit
                  weight: 'normal', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                }
              },
              tooltip: {
                enabled: true, // 是否启用,默认true
                backgroundColor: 'rgba(0, 0, 0, 0.8)', // 背景颜色
                titleColor: 'yellow', // 标题颜色
                titleFont: {
                  size: 50,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                },
                titleAlign: 'left', //标题对齐: 'left' (default),'right','center'
                titleMarginBottom: 6, // title底部的外间距
                bodyColor: 'green', // 主体颜色
                bodyFont: {
                  size: 50,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                },
                bodyAlign: 'left', //主体对齐: 'left' (default),'right','center'
                borderColor: 'rgba(0, 0, 0, 0)', // 边框颜色
                borderWidth: 2, // 边框宽度
                padding: 10,
                pointStyle:'rectRounded',
                usePointStyle: true
              },
              legend: {
                display: true, // true(default),false
                position: 'top', //'top'(default),'left','bottom','right','chartArea'
                align: 'center',  //'center'(default),'start','center','end'
                maxHeight: 100, // 最大高度
                maxWidth: 100, // 最大宽度
                onClick: () => {}, // 点击事件
                onHover: () => {}, // 经过
                onLeave: () => {}, // 离开
                reverse: false, // false(default),true时和dataset顺序相反
                // legend title
                title: {
                  color: 'red',
                  display: true,
                  padding: 10,
                  font: {}, // 字体和title中完全一致
                  text: 'legend title'
                }
              },

            },
            // animation: false, // 动画
            animation: {
              duration: 1000,
              easing:'linear',  // 过渡效果
              delay: 1000, // 毫秒,延时多久后显示动画
              loop: false, // 如果true, 动画将一直循环播
              onProgress: () => {console.log('ing')}, // 动画进行中事件
              onComplete: () => {console.log('done')}, // 动画完成事件
            },
            scales: {
              x: {
                display: true, // 默认true
                // x轴刻度相关内容
                ticks: {
                  display: true, // 默认true
                  color:'red', // 颜色
                  font: {}, // 字体和title中完全一致
                },
                position: 'left',
                title: {
                  display: true, // 默认false
                  text: 'Value',
                  color: '#191',
                  font: {
                    family: 'Times',
                    size: 20,
                    style: 'normal',
                    lineHeight: 1.2
                  },
                  padding: {top: 30, left: 0, right: 0, bottom: 0}
                },
                grid: {
                  display: true, // 是否显示,默认false
                  color:'green',
                  lineWidth: 2
                }
              },
              y: {
                display: true, // 默认true
                min: 0, // 最小值
                max: 200, // 最大值
                // y轴刻度相关内容
                ticks: {
                  display: true, // 默认true
                  color:'red', // 颜色
                  font:{},  // 字体和title中完全一致
                },
                position: 'left', //定位:left(default),right
                title: {
                  display: true, // 默认false
                  text: 'Value', // 文本
                  color: '#191', // 颜色
                  font: {
                    family: 'Times', 
                    size: 20,
                    style: 'normal',
                    lineHeight: 1.2
                  },
                  padding: {top: 30, left: 0, right: 0, bottom: 0}
                },
                grid: {
                  display: true, // 是否显示,默认true
                  color: 'red',
                  lineWidth: 2
                }
              },
              
            }
          }
      });
    },
    initChartLine () {
      var ctx = document.getElementById("myChartLine");
      new Chart(ctx, {
          type: 'line',
          data: {
              labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
              datasets: [
                {
                  label: 'one',
                  data: [12, 19, 32, 15, 62, 43], // 数值
                  fill: false, // 是否填充区域
                  borderWidth: 8,
                  borderColor: "red", // 背景颜色
                  hoverBorderColor: 'black', // 鼠标经过时背景颜色
              },
              {
                  label: 'two', // 系列名
                  data: [32, 59, 13, 75, 27, 13], // 数值
                  showLine: true, // 是否显示线,默认true
                  borderWidth: 2, // 边框宽度,即折线线的宽度
                  borderColor: "green", // 边框颜色
                  fill: true, // 是否填充,默认false,背景颜色即填充颜色
                  backgroundColor: 'yellow', // fill为true时生效, 背景颜色即填充颜色
                  pointStyle:'triangle', // 拐点样式,其它值有:'circle'(圆)、'cross'(十字)、'crossRot'(叉)、'dash'(短横杠),'line'(长横线),'rect'(矩形),'rectRounded'(圆角矩型),'rectRot'(菱形),'star'星星,'triangle'(三角形),false,
                  pointBackgroundColor: 'orange', // 拐点背景颜色
                  pointBorderColor: '#666', // 拐点边框颜色
                  pointBorderWidth: 20, // 拐点边框宽度
                  pointRotation: 30, // 拐点旋转角度
                  pointRadius: 5 //拐点半径
                },
            ]
          },
          options: {
            responsive: false, // 响应式
            plugins: {
              customCanvasBackgroundColor:{
                color: 'lightGreen'
              },
              tooltip: {
                enabled: true, // 是否启用,默认true
                backgroundColor: 'rgba(0, 0, 0, 0.8)', // 背景颜色
                titleColor: 'yellow', // 标题颜色
                titleFont: {
                  size: 50,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                },
                titleAlign: 'left', //标题对齐: 'left' (default),'right','center'
                titleMarginBottom: 6, // title底部的外间距
                bodyColor: 'green', // 主体颜色
                bodyFont: {
                  size: 50,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                },
                bodyAlign: 'left', //主体对齐: 'left' (default),'right','center'
                borderColor: 'rgba(0, 0, 0, 0)', // 边框颜色
                borderWidth: 2, // 边框宽度
                padding: 10
              },
              legend: {
                display: true, // true(default),false
                position: 'top', //'top'(default),'left','bottom','right','chartArea'
                align: 'center',  //'center'(default),'start','center','end'
                // 图例标签
                labels: {
                  color:'purple', // 标签颜色
                  boxWidth: 100, // 盒宽度(文字前面的图标)
                  boxHeight: 100, // 盒高度(文字前面的图标)
                  font: {},
                  padding: 10, //默认10
                  textAlign: 'center', //对齐方式 'center'(default),'left', 'right'
                  pointStyle : 'crossRot', // 点样式,文字前面的图标,usePointStyle为true时才生效,其它值有:'circle'(圆)、'cross'(十字)、'crossRot'(叉)、'dash'(短横杠),'line'(长横线),'rect'(矩形),'rectRounded'(圆角矩型),'rectRot'(菱形),'star'星星,'triangle'(三角形),false,
                  usePointStyle: true, // 是否使用点样式,默认false
                },
                maxHeight: 100, // 最大高度
                maxWidth: 100, // 最大宽度
                onClick: () => {}, // 点击事件
                onHover: () => {}, // 经过
                onLeave: () => {}, // 离开
                reverse: false, // false(default),true时和dataset顺序相反
                // legend title
                title: {
                  color: 'red',
                  display: true,
                  padding: 10,
                  font: {}, // 字体和title中完全一致
                  text: 'legend title'
                }
              },

            },
            // animation: false, // 动画
            animation: {
              duration: 1000,
              easing:'linear',  // 过渡效果
              delay: 1000, // 毫秒,延时多久后显示动画
              loop: false, // 如果true, 动画将一直循环播
              onProgress: () => {console.log('ing')}, // 动画进行中事件
              onComplete: () => {console.log('done')}, // 动画完成事件
            },
            scales: {
              x: {
                display: true, // 默认true
                // x轴刻度相关内容
                ticks: {
                  display: true, // 默认true
                  color:'red', // 颜色
                  font: {}, // 字体和title中完全一致
                },
                position: 'top',
                title: {
                  display: true, // 默认false
                  text: 'Value',
                  color: '#191',
                  font: {
                    family: 'Times',
                    size: 20,
                    style: 'normal',
                    lineHeight: 1.2
                  },
                  padding: {top: 30, left: 0, right: 0, bottom: 0}
                },
                grid: {
                  display: true, // 是否显示
                }
              },
              y: {
                display: true, // 默认true
                min: 0, // 最小值
                max: 200, // 最大值
                // y轴刻度相关内容
                ticks: {
                  display: true, // 默认true
                  color:'red', // 颜色
                  font:{},  // 字体和title中完全一致
                },
                position: 'right', //定位:left(default),right
                title: {
                  display: true, // 默认false
                  text: 'Value', // 文本
                  color: '#191', // 颜色
                  font: {
                    family: 'Times', 
                    size: 20,
                    style: 'normal',
                    lineHeight: 1.2
                  },
                  padding: {top: 30, left: 0, right: 0, bottom: 0}
                },
                grid: {
                  display: false, // 是否显示
                }
              },
            }

          }
      });
    },
    initChartPie () {
      var ctx = document.getElementById("myChartPie");
      new Chart(ctx, {
          type: 'pie',
          data: {
              labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
              datasets: [
                {
                  // label: 'one',
                  data: [12, 19, 3, 5, 2, 3],
                  backgroundColor: ["red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                  hoverBackgroundColor: 'black',
                  offset:100,
                  hoverOffset: 0,
                  borderWidth: 2,
                  borderColor: 'black',
                  hoverBorderColor: 'black',
                  borderAlign:'center'
              },
            ]
          },
          options: {
            responsive: false, // 响应式
            plugins: {
              customCanvasBackgroundColor:{
                color: 'lightGreen'
              },
              tooltip: {
                enabled: true, // 是否启用,默认true
                backgroundColor: 'rgba(0, 0, 0, 0.8)', // 背景颜色
                titleColor: 'yellow', // 标题颜色
                titleFont: {
                  size: 50,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                },
                titleAlign: 'left', //标题对齐: 'left' (default),'right','center'
                titleMarginBottom: 6, // title底部的外间距
                bodyColor: 'green', // 主体颜色
                bodyFont: {
                  size: 50,   // default:12
                  family: 'Helvetica Neue',  // default:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
                  style: 'italic', // 默认normal, italic, oblique, initial, inherit
                  weight: 'bold', //  normal | bold | bolder | lighter
                  lineHeight: 1.2 // default: 1.2
                },
                bodyAlign: 'left', //主体对齐: 'left' (default),'right','center'
                borderColor: 'rgba(0, 0, 0, 0)', // 边框颜色
                borderWidth: 2, // 边框宽度
                padding: 10
              },
              legend: {
                display: true, // true(default),false
                position: 'top', //'top'(default),'left','bottom','right','chartArea'
                align: 'center',  //'center'(default),'start','center','end'
                // 图例标签
                labels: {
                  color:'purple', // 标签颜色
                  boxWidth: 100, // 盒宽度(文字前面的图标)
                  boxHeight: 100, // 盒高度(文字前面的图标)
                  font: {},
                  padding: 10, //默认10
                  textAlign: 'center', //对齐方式 'center'(default),'left', 'right'
                  pointStyle : 'rectRot', // 点样式,文字前面的图标,usePointStyle为true时才生效,其它值有:'circle'(圆)、'cross'(十字)、'crossRot'(叉)、'dash'(短横杠),'line'(长横线),'rect'(矩形),'rectRounded'(圆角矩型),'rectRot'(菱形),'star'星星,'triangle'(三角形),false,
                  usePointStyle: true, // 是否使用点样式,默认false
                },
                maxHeight: 100, // 最大高度
                maxWidth: 100, // 最大宽度
                onClick: () => {}, // 点击事件
                onHover: () => {}, // 经过
                onLeave: () => {}, // 离开
                reverse: false, // false(default),true时和dataset顺序相反
                // legend title
                title: {
                  color: 'red',
                  display: true,
                  padding: 10,
                  font: {}, // 字体和title中完全一致
                  text: 'legend title'
                }
              },

            },
            // animation: false, // 动画
            animation: {
              duration: 1000,
              easing:'linear',  // 过渡效果
              delay: 1000, // 毫秒,延时多久后显示动画
              loop: false, // 如果true, 动画将一直循环播
              onProgress: () => {console.log('ing')}, // 动画进行中事件
              onComplete: () => {console.log('done')}, // 动画完成事件
            },
          }
      });
    },
  },
  mounted () {
    this.initChartBar();
    this.initChartLine();
    this.initChartPie();
  }
}
</script>
<style scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue2项目使用vue-chartjs,需要先安装以下依赖: ```bash npm install vue-chartjs chart.js ``` 然后在需要使用图表的组件,引入vue-chartjs并注册组件: ```javascript import { Line } from 'vue-chartjs' export default { extends: Line, mounted () { this.renderChart({ labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { label: 'Data One', backgroundColor: '#f87979', data: [40, 39, 10, 40, 39, 80, 40] } ] }, {responsive: true, maintainAspectRatio: false}) } } ``` 这里用Line为例,如果需要使用其他类型的图表,只需要替换掉对应的组件名即可。在mounted函数,我们可以通过调用`this.renderChart`方法来渲染图表,传入的第一个参数是图表的数据,第二个参数是配置项。 当然,你也可以在组件定义自己的数据和配置项,然后在mounted函数调用`this.renderChart`方法来渲染图表,例如: ```javascript import { Line } from 'vue-chartjs' export default { extends: Line, data () { return { chartData: { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { label: 'Data One', backgroundColor: '#f87979', data: [40, 39, 10, 40, 39, 80, 40] } ] }, chartOptions: { responsive: true, maintainAspectRatio: false } } }, mounted () { this.renderChart(this.chartData, this.chartOptions) } } ``` 这里我们将图表的数据和配置项定义在了组件的data。 最后,在需要使用该组件的地方,直接引入即可: ```html <template> <div> <my-chart></my-chart> </div> </template> <script> import MyChart from './MyChart.vue' export default { components: { MyChart } } </script> ``` 这样就可以在Vue2项目使用vue-chartjs了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值