Public
Edited
Mar 1, 2023
Insert cell
# 二维折线图-日期拆分-年、季度
Insert cell
main = html`<div style="height: 100%; min-height:300px;width:100%;"></div>`
Insert cell
dimensions = ["time", "TLJ", "province"]
Insert cell
finalDim = ["季度", "mergedDim"]
Insert cell
metrics = ['count']
Insert cell
data = [
{ time: "201901", TLJ: "广州局 ", province: "广东", count: 1.92098 },
{ time: "201902", TLJ: "广州局 ", province: "广东", count: 1.796244 },
{ time: "201903", TLJ: "广州局 ", province: "广东", count: 2.34 },
{ time: "201904", TLJ: "广州局 ", province: "广东", count: 2.5876 },
{ time: "201905", TLJ: "广州局 ", province: "广东", count: 1.873 },
{ time: "201906", TLJ: "广州局 ", province: "广东", count: 2.32907 },
{ time: "201907", TLJ: "广州局 ", province: "广东", count: 1.92098 },
{ time: "201908", TLJ: "广州局 ", province: "广东", count: 1.796244 },
{ time: "201909", TLJ: "广州局 ", province: "广东", count: 2.34 },
{ time: "201910", TLJ: "广州局 ", province: "广东", count: 2.5876 },
{ time: "201911", TLJ: "广州局 ", province: "广东", count: 1.873 },
{ time: "201912", TLJ: "广州局 ", province: "广东", count: 2.32907 },
{ time: "201901", TLJ: "武汉局 ", province: "湖北", count: 2.4056 },
{ time: "201902", TLJ: "武汉局 ", province: "湖北", count: 1.634908 },
{ time: "201903", TLJ: "武汉局 ", province: "湖北", count: 2.09865 },
{ time: "201904", TLJ: "武汉局 ", province: "湖北", count: 2.40977 },
{ time: "201905", TLJ: "武汉局 ", province: "湖北", count: 2.58026 },
{ time: "201906", TLJ: "武汉局 ", province: "湖北", count: 2.358026 },
{ time: "201901", TLJ: "上海局 ", province: "上海", count: 2.0 },
{ time: "201902", TLJ: "上海局 ", province: "上海", count: 2.55 },
{ time: "201903", TLJ: "上海局 ", province: "上海", count: 1.9 },
{ time: "201904", TLJ: "上海局 ", province: "上海", count: 2.3 },
{ time: "201905", TLJ: "上海局 ", province: "上海", count: 1.873 },
{ time: "201906", TLJ: "上海局 ", province: "上海", count: 2.32907 },
{ time: "201903", TLJ: "乌鲁木齐局 ", province: "新疆", count: 2.09865 },
{ time: "201904", TLJ: "乌鲁木齐局 ", province: "新疆", count: 2.440977 },
{ time: "201905", TLJ: "乌鲁木齐局 ", province: "新疆", count: 2.58026 },
{ time: "201906", TLJ: "乌鲁木齐局 ", province: "新疆", count: 1.658026 }
]
Insert cell
finalDat = {
let [mergedDim, quarterDim] = finalDim;
let [metric] = metrics;
let timeDim = dimensions[0];
return data.map((d) => {
let m = moment(d[timeDim], "YYYY-MM-DD");
return {
[metric]: d[metric],
[quarterDim]: m.format("[Q]Q"),
[mergedDim]: `${m.format("YYYY")}-${_.drop(dimensions, 1)
.map((dim) => _.trim(d[dim]))
.join("-")}`
};
});
}
Insert cell
finalDat2 = {
let [mergedDim, quarter] = finalDim;
return _(finalDat)
.groupBy(mergedDim)
.mapValues((arr) => _.groupBy(arr, quarter))
.value();
}
Insert cell
translationDict = ({})
Insert cell
metricsFormatDict = ({
[metrics[0]]: x => x
})
Insert cell
metricFormatterFn = metricsFormatDict[metrics[0]]
Insert cell
echartsLikeTooltipFormatter = (params) => {
let seriesName = params.seriesName;
let xVal = params.name;
let val = (_.isFunction(metricFormatterFn) ? metricFormatterFn : _.identity)(
params.value
);
let valHtml = `<span style='float:right;margin-left:10px;'>${val}</span>`;
return `${seriesName}<br /><span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:${params.color}' ></span>${xVal} : ${valHtml}`;
}
Insert cell
option = {
let [meargedDim, quarterDim] = finalDim;
let [metric] = metrics;
let quarters = _(finalDat).keyBy(quarterDim).keys().sort().value();
return {
tooltip: {
show: true,
trigger: "axis",

// trigger: "item",
// formatter: echartsLikeTooltipFormatter
// valueFormatter: (value) => value.toFixed(2) //添加 $ 前缀,保留2位小数
valueFormatter: (value) =>
(_.isFunction(metricFormatterFn) ? metricFormatterFn : _.identity)(
value
)
},
legend: {
type: "scroll",
icon: "circle",
zlevel: 2
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
zlevel: 0
},
xAxis: {
type: "category",
data: quarters,
//axisLabel: { rotate: 30 },
axisTick: { alignWithLabel: true },
axisLine: { onZero: false }
},
yAxis: {
type: "value",
boundaryGap: [0, 0.01],
scale: true, //非0
axisLabel: {
formatter: (value) => {
return (
_.isFunction(metricFormatterFn) ? metricFormatterFn : _.identity
)(value);
}
}
},
color: [
"#5470c6",
"#91cc75",
"#fac858",
"#ee6666",
"#73c0de",
"#3ba272",
"#fc8452",
"#9a60b4",
"#ea7ccc"
],
series: [
..._.map(finalDat2, (dict, group) => {
return {
name: group,
type: "line",
symbol: "circle",
symbolSize: 8,
//smooth: 0.3,
smooth: true,
data: _.map(quarters, (t) => _.sumBy(dict[t], metric) || null),
markPoint: {
symbol: "diamond",
symbolOffset: [0, "-50%"],
symbolSize: 60,
//symbolSize: (value: Array|number, params: data) => number|Array,
data: [
{
type: "max",
name: "Max",
label: { color: "#000", fontWeight: "bold" }
},
{
type: "min",
name: "Min",
label: { color: "#000", fontWeight: "bold" }
}
],
label: {
formatter: (params) => {
let metricFormatterFn =
metricsFormatDict[metrics[params.seriesIndex]] || _.identity;
return metricFormatterFn(params.value);
}
}
}
};
})
]
};
}
Insert cell
chartInst = echarts.init(main)
Insert cell
chartInst.setOption(option, { notMerge: true, lazyUpdate: true })
Insert cell
onEvents = ({})
Insert cell
function bindEvents(instance, events) {
function _bindEvent(eventName, func) {
if (eventName && func) {
instance.on(eventName, (param) => {
func(param, instance);
});
}
}

for (const eventName in events) {
_bindEvent(eventName, events[eventName]);
}
}
Insert cell
bindEvents(chartInst, onEvents || {}); //调用bindEvents
Insert cell
(width, chartInst.resize()) // 自适应宽度
Insert cell
echarts = window.echarts || require("https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js")
Insert cell
_ = window._ || require("lodash")
Insert cell
moment = window.moment || require("moment")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more