5

我正在使用 vue-chartjs 在 web 应用程序上呈现图形。我知道如果您使用原始,您可以打印图表。但是我不知道如何使用库的 vue 版本来做到这一点。

我在外部charts.js 文件中有我的图表变量

import {Bar, mixins } from 'vue-chartjs'
Chart.defaults.global
let chartOptions = Chart.defaults.global;   
const { reactiveProp } = mixins
export default {
   extends: Bar,
   mixins: [reactiveProp],
   props: ['options'],

   mounted () {
    let that = this;
    that.chartOptions = {
      scales: {
        yAxes: [{
            ticks: {
                suggestedMin: 0,  
                fontFamily: "'Overpass_Mono', 'Monaco', monospace",
                fontColor: "rgba(254, 255, 248, 0.5)"
            },
            gridLines: {
                color: 'rgba(255, 80, 248, 0.08)',
                zeroLineColor: 'rgb(168, 119, 181)',
                 zeroLineWidth: 2
            },
        }],
        xAxes: [{
            ticks: {
                suggestedMin: 0,    
                 fontColor: "rgb(168, 119, 181)"

            },
            gridLines: {
                color: 'rgba(255, 80, 248, 0.08)',
                zeroLineColor: 'transparent',
            }
        }],
    },
    legend: {
        labels: {
            fontColor: 'rgb(168, 119, 181)',
        }
    }
  },
    this.renderChart(this.chartData, that.chartOptions)
  }
}

然后在我的组件模板上我有:

<template>
    <div class="report">
        <charts v-if="todaySelected"
                :chart-id="'total_visits_chart_bar'" 
                :height="chartsHeight" 
                :options="chartOptions"
                :chart-data="datacollection" 
        ></charts>
        <div v-if="todaySelected">
        <button @click="printChart(charts)">Print chart</button>
    </div>
</template>
<script>
import charts from './chart_0.js'
    components: {
       charts,
    },
    data() {
        return{
             datacollection: {"datasets":[{"label":"Entries Today","data":[15,15,15,0]},{"label":"Currently Inside","data":[2,2,2,0]}],"labels":[]}
        }
     }.
 methods: {
     printChart(charts) {
        charts.print();
     },
  }
</script>

任何帮助,将不胜感激。

4

4 回答 4

2

答案是:是的,是的。您在组件脚本中的打印方法可能是:

methods:{
  printChart() {
    var canvasEle = document.getElementById('total_visits_chart_bar');
    var win = window.open('', 'Print', 'height=600,width=800');
    win.document.write("<br><img src='" + canvasEle.toDataURL() + "' />");
    setTimeout(function(){ //giving it 200 milliseconds time to load
            win.document.close();
            win.focus()
            win.print();
            win.location.reload()
    }, 200);  
  },
}

您还可以将其添加到组件的样式中:

@media print{
    @page {
        size: landscape
        }
}
于 2018-11-13T13:26:34.050 回答
2

vue-chartjs 基于 chart.js 而不是 canvas.js,因此它没有“内置”的打印方式。

您必须使用一些自定义逻辑和本机 javascript 打印功能来完成。

但是,您可以在图表组件中抓取画布元素并生成例如图像,然后打印该图像。

这会有点棘手,因为您只能访问图表组件内的画布。因此,您可能需要等待事件或道具触发toDataURL调用,然后将图像发送到您可以打印的父组件。如果要在父组件中触发打印。

methods: {
   print () {
     // grab the canvas and generate an image
     let image = this.$refs.canvas.toDataURL('image/png')
     // Emits an event with the image
     this.$emit('chart:print', image)
   }
}

在您的父组件中:

<template>
 <your-chart @chart:print="handlePrint"
<template/>

....
...

methods: {
 handlePrint(image) {
   const win = window.open('', 'Print', 'height=600, width=800')
   win.document.write(`<br><img src='${image}' />`)
   win.print()
   win.close()
 }
}
于 2018-11-14T09:54:14.593 回答
1

该库似乎基于 chartjs 而不是 canvasjs https://www.chartjs.org/docs/latest/您可能想研究如何打印窗口Quick Print HTML5 Canvas,并记住您可以访问 canvas 元素绘制图表的位置:

 methods: {
     printChart() {
        const canvasEle = this.$el.querySelector('canvas');
        //now your chart image is on canvasEle 
     },
  }
于 2018-11-12T22:47:54.890 回答
0

如果您不反对使用导出为 pdf 格式,则可以使用 jsPDF 库来实现此任务,例如:

<template>
    <div class="report">
        <charts v-if="todaySelected"
                :chart-id="'total_visits_chart_bar'" 
                :height="chartsHeight" 
                :options="chartOptions"
                :chart-data="datacollection" 
        ></charts>
    </div>
</template>
<script>
import jsPDF from 'jspdf'; //for PDF printing
methods: {
  pdfThatThing : function(){
      //Default export is a4 paper, portrait, using milimeters for units      
      let pdfName = 'test'; 
      var doc = new jsPDF();
      doc.text("Header", 20, 20); //at x,y at def.units 2cm
      //chart element
      let canvasEle = document.getElementById('total_visits_chart_bar');
      let chartURL = canvasEle.toDataURL(); //transform path 
      //a4 page is 209mm, adds at 4cm top, 2cm left, for 15cm in size      
      doc.addImage(chartURL, 'PNG', 20, 40, 150, 150 )
      doc.save(pdfName + '.pdf');
    },
}
</script>

还有在 pdf 查看器中自动显示打印对话框的选项:

doc.autoPrint({variant: 'non-conform'})
于 2020-01-03T12:48:41.583 回答