Javascript 如何使用HighCharts的导出CSV插件将长字符串转换为CSV
这就是我尝试使用的方法:Chart.getCSV 注意,我没有使用HighCharts丑陋的菜单下拉功能。他们的chart.options.exporting.enabled在我们的应用程序中设置为false。我们在另一个组件中有另一个按钮,其目的是从图表创建CSV 根据文档,我只需要在图表对象本身上调用.getCSVJavascript 如何使用HighCharts的导出CSV插件将长字符串转换为CSV,javascript,csv,highcharts,Javascript,Csv,Highcharts,这就是我尝试使用的方法:Chart.getCSV 注意,我没有使用HighCharts丑陋的菜单下拉功能。他们的chart.options.exporting.enabled在我们的应用程序中设置为false。我们在另一个组件中有另一个按钮,其目的是从图表创建CSV 根据文档,我只需要在图表对象本身上调用.getCSV return priceLine .then(alertSeries) .then(tagLine) .then(renderChart(chart)) .then((chart
return priceLine
.then(alertSeries)
.then(tagLine)
.then(renderChart(chart))
.then((chart) => {
// const pricelineData = chart.get('series-priceline').data;
// chart.options.navigator.series.data = pricelineData;
const csv = chart.getCSV();
const array = csv.split(',');
console.log(csv)
console.log('array', array)
ChartExport.storeChart(chart);
this.chartLoading = false;
return chart;
});
图表对象:
但是当我这样做的时候,我看到的是一个1长的字符串,如果我在注销HTML中看到它,你会如何将它转换成CSV?然后下载它
数组常量数组=csv.split',':
他们的.getTable将在console.log中生成此HTML。有没有简单的方法将其转换为可下载的CSV文件
啊,所以他们的文档中没有关于如何做到这一点的帮助,但我查看了他们的源代码,发现:
/**
* Call this on click of 'Download CSV' button
*/
Highcharts.Chart.prototype.downloadCSV = function () {
var csv = this.getCSV(true);
getContent(
this,
'data:text/csv,\uFEFF' + encodeURIComponent(csv),
'csv',
csv,
'text/csv'
);
};
/**
* Call this on click of 'Download XLS' button
*/
Highcharts.Chart.prototype.downloadXLS = function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">' +
'<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>' +
'<x:Name>Ark1</x:Name>' +
'<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->' +
'<style>td{border:none;font-family: Calibri, sans-serif;} .number{mso-number-format:"0.00";}</style>' +
'<meta name=ProgId content=Excel.Sheet>' +
'<meta charset=UTF-8>' +
'</head><body>' +
this.getTable(true) +
'</body></html>',
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s))); // #50
};
getContent(
this,
uri + base64(template),
'xls',
template,
'application/vnd.ms-excel'
);
};
现在我需要做的就是清除空序列,修正时间格式并重命名文件
this.downloadCsv = () => {
const csv = ChartExport.getChart().getCSV();
console.log(csv);
this.$close();
ChartExport.getChart().downloadXLS();
ChartExport.getChart().downloadCSV();
};