Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lightweight_charts/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,19 @@ def vertical_span(
return VerticalSpan(self, start_time, end_time, color)


class Line(SeriesCommon):
def __init__(self, chart, name, color, style, width, price_line, price_label, price_scale_id=None, crosshair_marker=True):

class Line(SeriesCommon):
def __init__(self, chart, name, color, style, width, price_line, price_label, group, price_scale_id=None, crosshair_marker=True):
super().__init__(chart, name)
self.color = color
self.group = group # Store group for potential internal use

# Pass group as part of the options if createLineSeries handles removing it
self.run_script(f'''
{self.id} = {self._chart.id}.createLineSeries(
"{name}",
{{
group: '{group}',
color: '{color}',
lineStyle: {as_enum(style, LINE_STYLE)},
lineWidth: {width},
Expand Down Expand Up @@ -832,17 +835,19 @@ def fit(self):
"""
self.run_script(f'{self.id}.chart.timeScale().fitContent()')

def create_line(
def create_line(
self, name: str = '', color: str = 'rgba(214, 237, 255, 0.6)',
style: LINE_STYLE = 'solid', width: int = 2,
price_line: bool = True, price_label: bool = True, price_scale_id: Optional[str] = None
price_line: bool = True, price_label: bool = True, group: str = '',
price_scale_id: Optional[str] =None
) -> Line:
"""
Creates and returns a Line object.
"""
self._lines.append(Line(self, name, color, style, width, price_line, price_label, price_scale_id))
self._lines.append(Line(self, name, color, style, width, price_line, price_label, group, price_scale_id ))
return self._lines[-1]


def create_histogram(
self, name: str = '', color: str = 'rgba(214, 237, 255, 0.6)',
price_line: bool = True, price_label: bool = True,
Expand All @@ -854,6 +859,7 @@ def create_histogram(
return Histogram(
self, name, color, price_line, price_label,
scale_margin_top, scale_margin_bottom)


def create_area(
self, name: str = '', top_color: str ='rgba(0, 100, 0, 0.5)',
Expand All @@ -868,6 +874,8 @@ def create_area(
width, price_line, price_label, price_scale_id))

return self._lines[-1]


def create_bar(
self, name: str = '', up_color: str = '#26a69a', down_color: str = '#ef5350',
open_visible: bool = True, thin_bars: bool = True,
Expand Down
42 changes: 34 additions & 8 deletions src/general/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export interface Scale{
height: number,
}


interface MultiLineOptions extends DeepPartial<LineStyleOptions & SeriesOptionsCommon> {
group?: string; // Define group as an optional string identifier
}
globalParamInit();
declare const window: GlobalParams;

Expand Down Expand Up @@ -179,16 +181,40 @@ export class Handler {
return volumeSeries;
}

createLineSeries(name: string, options: DeepPartial<LineStyleOptions & SeriesOptionsCommon>) {
const line = this.chart.addLineSeries({...options});
createLineSeries(
name: string,
options: MultiLineOptions
): { name: string; series: ISeriesApi<SeriesType> } {
const { group, ...lineOptions } = options;
const line = this.chart.addLineSeries(lineOptions);
this._seriesList.push(line);
this.legend.makeSeriesRow(name, line)
return {
name: name,
series: line,

// Get color of the series for legend display
const color = line.options().color || 'rgba(255,0,0,1)'; // Default to red if no color is defined
const solidColor = color.startsWith('rgba') ? color.replace(/[^,]+(?=\))/, '1') : color;

if (!group || group === '') {
// No group: create a standalone series row
this.legend.makeSeriesRow(name, line);
} else {
// Check if the group already exists
const existingGroup = this.legend._groups.find(g => g.name === group);

if (existingGroup) {
// Group exists: add the new line's name and color to the `names` and `solidColors` arrays
existingGroup.names.push(name);
existingGroup.seriesList.push(line);
existingGroup.solidColors.push(solidColor);
} else {
// Group does not exist: create a new one
this.legend.makeSeriesGroup(group, [name], [line], [solidColor]);
}
}

return { name, series: line };
}



createHistogramSeries(name: string, options: DeepPartial<HistogramStyleOptions & SeriesOptionsCommon>) {
const line = this.chart.addHistogramSeries({...options});
this._seriesList.push(line);
Expand Down
Loading