Skip to content

Add support for plotting lines on scatter charts  #1983

Closed
@Zncl2222

Description

@Zncl2222

Description

Recently, I tried to plot multiple line series on a single chart with a dual Y-axis in different scale. However, I found that the Secondary argument in ChartAxis doesn't allow assigning specific line series to the secondary Y-axis. After some investigation, I realized that it might take some time to implement this, so I decided to use a "scatter plot with lines" and set the secondary Y-axis on the scatter plot instead. However, it appears that the current version does not support drawing lines on scatter plots.

Therefore, I would like to request adding support for plotting lines on scatter charts to provide more flexibility in usage. I have already implemented the code for this feature. If you think this is a good idea, I am ready to submit a pull request. Thank you!

Below is an example of the code snippet I used to plot lines on a scatter chart in my current implementation..

func main() {
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()
	for idx, row := range [][]interface{}{
		{nil, "Apple", "Orange", "Pear"},
		{"Small", 2, 3, 3},
		{"Normal", 5, 2, 4},
		{"Large", 6, 7, 8},
	} {
		cell, err := excelize.CoordinatesToCellName(1, idx+1)
		if err != nil {
			fmt.Println(err)
			return
		}
		if err := f.SetSheetRow("Sheet1", cell, &row); err != nil {
			fmt.Println(err)
			return
		}
	}
	if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
		Type: excelize.Scatter,
		Series: []excelize.ChartSeries{
			{
				Name:       "Sheet1!$A$2",
				Categories: "Sheet1!$B$1:$D$1",
				Values:     "Sheet1!$B$2:$D$2",
				Line:       excelize.ChartLine{ShowScatterLine: true},  // Set ShowScatterLine to true
				Marker:     excelize.ChartMarker{Symbol: "none"},
				Fill:       excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"00FF00"}},
			},
			{
				Name:       "Sheet1!$A$3",
				Categories: "Sheet1!$B$1:$D$1",
				Values:     "Sheet1!$B$3:$D$3",
				Line:       excelize.ChartLine{ShowScatterLine: true},
			},
			{
				Name:       "Sheet1!$A$4",
				Categories: "Sheet1!$B$1:$D$1",
				Values:     "Sheet1!$B$4:$D$4",
			},
		},
	}); err != nil {
		fmt.Println(err)
		return
	}
	if err := f.SaveAs("Book1.xlsx"); err != nil {
		fmt.Println(err)
	}
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions