Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add several features for xlsx-chart library #19

Open
wants to merge 32 commits into
base: master
Choose a base branch
from

Conversation

Tsenzuk
Copy link

@Tsenzuk Tsenzuk commented Jul 20, 2021

Features that are added in this pr:

  • custom chart position on the spreadsheet
  • custom color (fill and border) for each point/bar/sector resolves Charts colors #15
  • support "noFill" value as a color to hide some point/bar/sector
  • custom color (fill and border) for each series
  • custom legend position
  • an ability to hide the legend
  • custom plot size and position in the chart block
  • custom title position in the chart block
  • clear multi-layer support to be able to add any amount of same type charts (e.g. for bar + bar stacked + column + column staked) resolves Multiple charts only works for column chart #17 resolves Does this package still works? #11
  • support for stacked charts

config for check:

let fs = require ("fs");
let XLSXChart = require ("xlsx-chart");
let xlsxChart = new XLSXChart ();
let opts = {
    file: "mult_"+Date.now(),
    charts: [
        {
            position: {
                fromColumn: 10,
                toColumn: 20,
                fromRow: 10,
                toRow: 20,
            },
            customColors: {
                points: {
                    "Title 1": {
                        "Field 1": 'FF0000',
                        "Field 2": '00FF00',
                    },
                },
                series: {
                    "Title 2": {
                        fill: 'aaaaaa',
                        line: '00ffff',
                    }
                }
            },
            chart: "bar",
            titles: [
                "Title 1",
                "Title 2",
                "Title 3"
            ],
            fields: [
                "Field 1",
                "Field 2",
                "Field 3",
                "Field 4"
            ],
            data: {
                "Title 1": {
                    "Field 1": 5,
                    "Field 2": 10,
                    "Field 3": 15,
                    "Field 4": 20
                },
                "Title 2": {
                    "Field 1": 10,
                    "Field 2": 5,
                    "Field 3": 20,
                    "Field 4": 15
                },
                "Title 3": {
                    "Field 1": 20,
                    "Field 2": 15,
                    "Field 3": 10,
                    "Field 4": 5
                }
            },
            chartTitle: "Title 1"
        },
        {
            // position: {
            //     fromColumn: 10,
            //     toColumn: 20,
            //     // fromRow: 10,
            //     // toRow: 20,
            // },
            // customColors: {
            //     points: {
            //         "Title 1": {
            //             "Field 1": 'FF0000',
            //             "Field 2": '00FF00',
            //         },
            //     },
            //     series: {
            //         "Title 2": {
            //             fill: 'aaaaaa',
            //             line: '00ffff',
            //         }
            //     }
            // },
            // chart: "line",
            titles: [
                "Title 1",
                "Title 2",
                "Title 3",
                "Title 4",
                "Title 5",
                "Title 6",
                "Title 7",
            ],
            fields: [
                "Field 1",
                "Field 2",
                "Field 3",
            ],
            data: {
                "Title 1": {
                    "chart": "bar",
                    grouping: "stacked",
                    "Field 1": 5,
                    "Field 2": 10,
                    "Field 3": 15,
                },
                "Title 2": {
                    "chart": "bar",
                    grouping: "stacked",
                    "Field 1": 10,
                    "Field 2": 5,
                    "Field 3": 20,
                },
                "Title 3": {
                    "chart": "line",
                    grouping: "stacked",
                    "Field 1": 20,
                    "Field 2": 15,
                    "Field 3": 10,
                },
                "Title 4": {
                    "chart": "bar",
                    "Field 1": 5,
                    "Field 2": 10,
                    "Field 3": 15,
                },
                "Title 5": {
                    "chart": "bar",
                    "Field 1": 10,
                    "Field 2": 5,
                    "Field 3": 20,
                },
                "Title 6": {
                    "chart": "column",
                    "Field 1": 20,
                    "Field 2": 15,
                    "Field 3": 10,
                },
                "Title 7": {
                    "chart": "line",
                    grouping: "stacked",
                    "Field 1": 15,
                    "Field 2": 10,
                    "Field 3": 5,
                },
            },
            chartTitle: "Chart Title 2"
        },
        {
            position: {
                fromColumn: 10,
                toColumn: 15,
                fromRow: 0,
                toRow: 10,
            },
            chart: "doughnut",
            titles: [
                "Price"
            ],
            fields: [
                "value",
                "rest",
                "down"
            ],
            data: {
                "Price": {
                    "value": 10,
                    "rest": 100 - 10,
                    "down": 100
                }
            },
            customColors: {
                points: {
                    "Price": {
                        "down": 'noFill'
                    }
                }
            },
            manualLayout: {
                plotArea: {
                    w: 0.5,
                    h: 0.5,
                    x: 0.25,
                    y: 0.33,
                },
                title: {
                    x: 0.4,
                    y: 0.6
                }
            },
            firstSliceAng: 270,
            holeSize: 50,
            legendPos: null,
            chartTitle: "Gauge\nchart"
        },
    ]
};
xlsxChart.generate (opts, function (err, data) {
    fs.writeFileSync (`.\\results\\${opts.file}.xlsx`, data);
});

image

"charts.xslx" template is absent in "templates" folder. Current implementation allow to use any template, that have axes, so bar is good enough
before fix zero values also set to empty string, same as values not passed at all
@mpaustin1993
Copy link

@Tsenzuk Awesome contribution! I've been using some of the features you implemented for a personal project of mine.

Upon chart / table export, do you have any idea how to align the table columns, or change the styling of the table headers?

I've been sifting through the documentation of DocumentFormat.OpenXml.Spreadsheet but the styling aspects of it are pretty confusing.

Thank you!

@Tsenzuk
Copy link
Author

Tsenzuk commented Jan 9, 2022

@mpaustin1993 yes, it's possible. @vshovkuta investigated this functionality. Here some code examples:

				var c = [{
					$: {
						r: "A" + (y + 2),
						t: "s",
						s: f === "gaugeRestValue" || f === "gaugeDownValue" ? "2" : "0" // here based on f variable we're setting the style with index === 2
					},
					v: me.getStr (f)
				}];
		me.read ({file: "xl/styles.xml"}, function (err, o) {
			if (err) {
				return cb (new VError (err, "writeTableStyle"));
			}

			o.styleSheet.cellXfs = {
				"$": {
					"count": "3"
				},
				"xf": [
					{
						"$": {
							"numFmtId": "0", "fontId": "0", "fillId": "0", "borderId": "0", "xfId": "0"
						}
					},
					{
						"$": {
							"numFmtId": "0", "fontId": "0", "fillId": "0", "borderId": "0", "xfId": "0", "applyAlignment": "1"
						},
						"alignment": {
							"$": {
								"vertical": "bottom", "wrapText": "1"
							}
						}
					},
					{ // it's a style added to styles array. It will be applied if it's index is used in "s" field of cell
						"$": {
							"numFmtId": "0", "fontId": "1", "fillId": "0", "borderId": "0", "xfId": "0", "applyFont": "1"
						}
					}
				]
			}

			const defaultFont = Object.assign ({}, o.styleSheet.fonts.font);

			o.styleSheet.fonts.font = [];
			o.styleSheet.fonts.font.push(defaultFont);

			const setWhiteColorForFont = Object.assign ({}, defaultFont);
			setWhiteColorForFont.color = {
				$: {
					rgb: "FFFFFFFF",
				}
			};

			o.styleSheet.fonts.font.push(setWhiteColorForFont);
			o.styleSheet.fonts.$.count = o.styleSheet.fonts.font.length;


			me.write ({file: "xl/styles.xml", object: o});
			cb ();
		});
	},

@mpaustin1993
Copy link

@Tsenzuk & @vshovkuta - thanks a bunch! I'll give this a try & let you know if I have any questions

@HaiAlison
Copy link

@mpaustin1993 yes, it's possible. @vshovkuta investigated this functionality. Here some code examples:

can you give me an advise to fix this error?
TypeError: Cannot set properties of undefined (setting 'c:ser')

@Tsenzuk
Copy link
Author

Tsenzuk commented Jun 29, 2022

@HaiAlison do you use the code from this branch or from the master?

if this branch, please also mention, which line it is?

Looks like, the object of the chart description inside the writeChart() is not initialized properly

@HaiAlison
Copy link

Oh, I have resolved this bug, thanks for your commit 00bf5ca

@rohanke
Copy link

rohanke commented Dec 3, 2022

let fs = require ("fs");
let XLSXChart = require ("xlsx-chart");
let xlsxChart = new XLSXChart ();

let opts = {
charts: [
{
position: {
fromColumn: 10,
toColumn: 20,
fromRow: 10,
toRow: 20,
},
customColors: {
points: {
"Title 1": {
"Field 1": 'FF0000',
"Field 2": '00FF00',
},
},
series: {
"Title 2": {
fill: 'ff0000',
line: '00ffff',
}
}
},
chart: "column",
titles: [
"Title 1",
"Title 2",
"Title 3"
],
fields: [
"Field 1",
"Field 2",
"Field 3",
"Field 4"
],
data: {
"Title 1": {
"Field 1": 5,
"Field 2": 10,
"Field 3": 15,
"Field 4": 20
},
"Title 2": {
"Field 1": 10,
"Field 2": 5,
"Field 3": 20,
"Field 4": 15
},
"Title 3": {
"Field 1": 20,
"Field 2": 15,
"Field 3": 10,
"Field 4": 5
}
},
chartTitle: "Title 1"
}
, {
chart: "column",
titles: [
"Title 1",
"Title 2",
"Title 3"
],
fields: [
"Set 1",
"Set 2",
"Set 3",
"Set 4"
],
data: {
"Title 1": {
"Set 1": 5,
"Set 2": 10,
"Set 3": 15,
"Set 4": 20
},
"Title 2": {
"Set 1": 10,
"Set 2": 5,
"Set 3": 20,
"Set 4": 15
},
"Title 3": {
"Set 1": 20,
"Set 2": 15,
"Set 3": 10,
"Set 4": 5
}
},
chartTitle: "Bisevo 2018 #2"
}, {
chart: "column",
titles: [
"Title 1",
"Title 2"
],
fields: [
"Set 1",
"Set 2",
"Set 3"
],
data: {
"Title 1": {
"Set 1": 15,
"Set 2": 30,
"Set 3": 45
},
"Title 2": {
"Set 1": 5,
"Set 2": 2,
"Set 3": 10
}
},
chartTitle: "Affärs-Utveckling 2020"
}]
};

xlsxChart.generate (opts, function (err, data) {
fs.writeFileSync ("zzzz.xlsx", data);
});

One I changed the column colors its always getting from defult colors Library - rohan

@Tsenzuk
Copy link
Author

Tsenzuk commented Dec 3, 2022

@rohanke
image
colors set only for first chart and looks like set correctly
It's expected that each chart has separate colors description

@rohanke
Copy link

rohanke commented Dec 4, 2022

you can see the below source I have set colours correctly but its now working as I expected . I only need to change colour of the column. I am almost spent more than 10 hours . Pls help me to figure it out

version : "xlsx-chart": "^0.4.3"

let fs = require ("fs");
let XLSXChart = require ("xlsx-chart");
let xlsxChart = new XLSXChart ();

let opts = {
charts: [
{
position: {
fromColumn: 10,
toColumn: 20,
fromRow: 10,
toRow: 20,
},
customColors: {
points: {
"Title 1": {
"Field 1": 'FF0000',
"Field 2": '00FF00',
},
},
series: {
"Title 2": {
fill: 'ff0000',
line: '00ffff',
}
}
},
chart: "column",
titles: [
"Title 1",
"Title 2",
"Title 3"
],
fields: [
"Field 1",
"Field 2",
"Field 3",
"Field 4"
],
data: {
"Title 1": {
"Field 1": 0,
"Field 2": 10,
"Field 3": 15,
"Field 4": 20
},
"Title 2": {
"Field 1": 10,
"Field 2": 5,
"Field 3": 20,
"Field 4": 15
},
"Title 3": {
"Field 1": 20,
"Field 2": 15,
"Field 3": 10,
"Field 4": 5
}
},
chartTitle: "Title 1"
}
]
};

xlsxChart.generate (opts, function (err, data) {
fs.writeFileSync ("settimeChart.xlsx", data);
});

@Tsenzuk
Copy link
Author

Tsenzuk commented Dec 4, 2022

@rohanke you're in the discussion about PR, that still wasn't merged.
If you need custom colors from this PR, you should use the code from the branch instead of release in npm

@rohanke
Copy link

rohanke commented Dec 4, 2022

Okey Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple charts only works for column chart Charts colors Does this package still works?
4 participants