我有一个简单的条形图,上面叠加了一条线。条形和线是由不同的数据生成的,因此图表会歪曲条形的大小,因为线数据明显大于条形数据。我想在图表右侧为线条设置单独的 y 轴以防止这种情况发生,但我找不到有关如何执行此操作的 go-echarts 文档。
这是图表当前的图像。如您所见,条形几乎看不出来。
下面是我的代码。该行添加到代码底部。有人知道如何在该行的右侧添加 y 轴吗?
// Sort the dates so that the x-axis is ordered.
var dates []string
for date := range data {
dates = append(dates, date)
}
sort.Strings(dates)
// Build series data for the bars (profit) and line (transaction count).
var barData []opts.BarData
for _, date := range dates {
stat := data[date]
barData = append(barData, opts.BarData{Value: stat.Profit})
}
// Create the bar chart for profit.
bar := charts.NewBar()
bar.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "Daily Profit",
Subtitle: "Profit in USDT",
}),
charts.WithLegendOpts(opts.Legend{
Show: opts.Bool(false),
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "slider",
Start: 50,
End: 100,
}),
// First y-axis (index 0) for Profit USDT; no explicit index needed.
charts.WithYAxisOpts(
opts.YAxis{
Type: "value",
},
),
)
bar.SetXAxis(dates).AddSeries("Profit USDT", barData)
// Build series data for the bars (profit) and line (transaction count).
var lineData []opts.LineData
for _, date := range dates {
stat := data[date]
lineData = append(lineData, opts.LineData{Value: stat.Count, YAxisIndex: 1})
}
// Create the bar chart for profit.
line := charts.NewLine()
line.SetXAxis(dates).
AddSeries("Transaction Count", lineData)
line.SetGlobalOptions(
// First y-axis (index 0) for Profit USDT; no explicit index needed.
charts.WithYAxisOpts(
opts.YAxis{
Type: "value",
Position: "right",
},
),
)
line.ExtendYAxis(opts.YAxis{Type: "value", Position: "right", Show: opts.Bool(true)})
bar.Overlap(line)
return bar