Browse Source

线状图组件修改

feature
mk 3 years ago
parent
commit
05fe6d6abf
  1. 308
      src/views/modules/visual/cpts/line-chart.vue

308
src/views/modules/visual/cpts/line-chart.vue

@ -1,13 +1,12 @@
<template>
<div class="m-chart">
<canvas :id="id" :width="width" :height="height" />
<div :id="id" style="width: 400px; height: 200px"></div>
</div>
</template>
<script>
import nextTick from "dai-js/tools/nextTick";
import F2 from "@antv/f2/lib/index-all";
import * as echarts from "echarts";
// iosbug
const fontFamily = "PingFang SC";
const fontSize = 14;
@ -16,195 +15,6 @@ let id = "myChart" + new Date().getDate();
let chart;
let srcData = [];
let textShapeList = [];
const addTextShape = () => {
if (textShapeList.length > 0) {
textShapeList.forEach((item) => {
item.remove(true);
});
textShapeList = [];
}
// 线
const canvas = chart.get("canvas");
const group = canvas.addGroup();
const shapes = {};
function addFn(list) {
const listLength = list.length;
list.forEach(function (obj, index) {
//
if (listLength > 7 && index > 0 && index < listLength - 1) {
if (index % Math.ceil(listLength / 5) != 0) {
return;
}
}
const offsetX = 6;
const offsetY = 1;
const point = chart.getPosition(obj);
const text = group.addShape("text", {
attrs: {
x: obj.type === "支持" ? point.x + 2 * offsetX : point.x - offsetX,
y: point.y + offsetY,
text: obj.value,
textAlign: "center",
textBaseline: "bottom",
fill: "#333",
fontWeight: 500,
fontFamily,
fontSize,
},
});
textShapeList.push(text); // shape, 便
});
}
srcData.sort((a, b) => {
return new Date(a.date).getTime() - new Date(b.date).getTime();
});
let supData = srcData.filter((item) => item.type == "支持");
let oppData = srcData.filter((item) => item.type == "反对");
addFn(supData);
addFn(oppData);
};
const iniChart = function (config, srcData) {
chart = new F2.Chart({
id,
...config,
});
const supColor = "#FFDA0E";
const oppColor = "#00E5ED";
chart.source(srcData, {
date: {
range: [0, 1],
type: "timeCat",
mask: "MM-DD",
},
value: {
// type: "linear",
},
});
//
chart.axis("date", {
line: {
lineWidth: 1,
stroke: "#E7EEEE",
},
label: {
fontWeight: 500,
fontSize,
fill: "#B5B7BF",
textBaseline: "middle",
},
labelOffset: 25,
country: {
range: [0.1, 0.9], // range 使
},
grid: null,
});
chart.axis("value", {
line: {
lineWidth: 1,
stroke: "#E7EEEE",
},
labelOffset: 20,
label: {
fontWeight: 500,
fontSize,
fill: "#B5B7BF",
},
});
chart.tooltip({
custom: true, // tooltip
showXTip: true, // X
showYTip: true, // Y
snap: true, // 线
crosshairsType: "xy", // 线
crosshairsStyle: {
// 线
lineDash: [2], // 线
stroke: "rgba(255, 0, 0, 0.25)",
lineWidth: 20,
},
});
//
chart.legend({
position: "bottom",
align: "center",
offsetY: -30,
custom: true,
nameStyle: {
fill: "#999",
fontWeight: 500,
fontSize,
},
itemWidth: 150,
wordSpace: 25,
items: [
{
name: "支持",
marker(x, y, r, ctx) {
ctx.lineWidth = 10;
ctx.strokeStyle = supColor;
ctx.moveTo(x - r - 15, y);
ctx.lineTo(x + r + 15, y);
ctx.stroke();
ctx.fill();
},
},
{
name: "反对",
marker(x, y, r, ctx) {
ctx.lineWidth = 10;
ctx.strokeStyle = oppColor;
ctx.moveTo(x - r - 15, y);
ctx.lineTo(x + r + 15, y);
ctx.stroke();
ctx.fill();
},
},
],
});
// 线
// chart.tooltip({
// showCrosshairs: true, // Tooltip 线
// shared: true,
// });
// // new Add -- tooltip:change
// chart.on('tooltip:change', ev => {
// // ev
// hoverData = ev.data.items[0].data;
// })
// // new Add -- plot
// chart.on('plot:click', ev => {
// // tooltiphoverDatahoverData线
// console.log(JSON.stringify(hoverData));
// })
chart
.line()
.position("date*value")
.color("type", (type) => {
if (type === "支持") {
return supColor;
}
return oppColor;
})
.style("type", {
lineWidth: 1,
})
.animate();
chart.render();
addTextShape();
// chart return
return chart;
};
export default {
name: "chart",
props: {
@ -237,25 +47,121 @@ export default {
},
watch: {
list(data) {
chart.changeData(data);
this.initCharts();
srcData = data;
addTextShape();
},
},
async created() {
await nextTick(200);
console.log(this.config, "config");
console.log(this.list, "list");
iniChart(this.config, this.list);
//
await nextTick(200);
this.iniLoading = true;
},
mounted() {
console.log(this.config, "config");
console.log(this.list, "list");
let date = this.list
.filter((item) => item.type == "支持")
.map((item) => item.date);
let data = this.list
.filter((item) => item.type == "支持")
.map((item) => item.value);
let data1 = this.list
.filter((item) => item.type == "反对")
.map((item) => item.value);
console.log(data);
nextTick(1000);
this.initCharts(date, data, data1);
},
methods: {
setList(data) {
chart.changeData(data);
srcData = data;
addTextShape();
// date x data data1
initCharts(date, data, data1) {
console.log(this.id);
let div = document.getElementById(this.id);
let myChart = echarts.init(div);
var option = {
tooltip: {
trigger: "axis",
},
legend: {
data: ["支持", "反对"],
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
toolbox: {
feature: {
saveAsImage: {},
},
},
dataZoom: [
{
type: 'slider',
realtime: true,
start: 0,
// end: 20, // 0 ~ 100
height: 5, //
left: '25%', //
right: '25%', //
bottom: '12%', //
show: true, //
fillerColor: "rgba(2,110,252,1)", //
borderColor: "rgba(17, 100, 210, 0.12)",
backgroundColor: 'rgba(232,244,255,0.51)',//
handleSize: 0, //
showDetail: false, //
zoomLock: true, //
moveOnMouseMove: false, //
//zoomOnMouseWheel: false, //
//****
startValue: 0, //
endValue: 3, // 5
// minValueSpan: 5, //
// maxValueSpan: 5, //
},
{
type: "inside", //
start: 0,
// end: 20,
startValue: 0, //
endValue: 5, // 5
zoomOnMouseWheel: false, //
moveOnMouseWheel: true, //
moveOnMouseMove: true //
}
],
xAxis: {
type: "category",
boundaryGap: false,
data: date,
},
yAxis: {
type: "value",
minInterval: "1",
},
series: [
{
name: "支持",
type: "line",
data: data,
},
{
name: "反对",
type: "line",
data: data1,
},
],
};
myChart.setOption(option);
window.addEventListener("resize", () => myChartPieLeft.resize(), false);
},
},
};

Loading…
Cancel
Save