import type { ResolvedChartOptions } from "../types/ChartOptions";
/**
* Creates SVG title element
* @param config - Resolved chart options
* @returns SVG string for the title
*/
export const createTitle = (config: ResolvedChartOptions): string => {
return ` ${config.title}\n`;
};
/**
* Create the Y-axis and grid lines for the chart
* @param config - Resolved chart options
* @param maxValue - Maximum value for the Y-axis
* @returns SVG string for the Y-axis and grid lines
*/
export const createYAxis = (
config: ResolvedChartOptions,
maxValue: number,
): string => {
// Initialize SVG string
let svg = "";
// Number of steps for the y-axis
const yAxisSteps = 10;
// Pre-compute values
const { width, height, margin } = config;
const chartWidth = width - margin.left - margin.right;
const chartHeight = height - margin.top - margin.bottom;
for (let i = 0; i <= yAxisSteps; i++) {
const y = chartHeight - (i / yAxisSteps) * chartHeight;
const value = (i / yAxisSteps) * maxValue;
const formattedValue = value.toFixed(config.decimalPlaces);
// Grid line
svg += ` \n`;
// Y-axis label
svg += ` ${formattedValue}\n`;
}
// Y-axis title
svg += ` ${config.yAxisLabel}\n`;
// X-axis line
svg += ` \n`;
return svg;
};
/**
* Create the legend for the chart
* @param config - Resolved chart options
* @param items - Array of item names for the legend
* @returns SVG string for the legend
*/
export const createLegend = (
config: ResolvedChartOptions,
items: string[],
): string => {
// Initialize SVG string
let svg = "";
// Pre-compute values
const chartWidth = config.width - config.margin.left - config.margin.right;
const legendX = chartWidth + 10;
let legendY = 0;
// Legend title
svg += ` ${
config.legendTitle
}\n`;
legendY += 25;
items.forEach((item, index) => {
const y = legendY + index * 25;
const colorIndex = index % config.colors.length;
// Legend color box
svg += ` \n`;
// Legend text
svg += ` ${item}\n`;
});
return svg;
};