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

Easier custom style and an example for it #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion svg/charts/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,13 @@ def calculate_graph_dimensions(self):
def load_resource_stylesheet(name, subs=None):
if subs is None:
subs = dict()
template = importlib_resources.read_text('svg.charts', name)
try:
# attempt read from library files
template = importlib_resources.read_text('svg.charts', name)
except FileNotFoundError:
# attempt read from calling code local files
with open(name, "r") as f:
template = f.read()
source = template % subs
return cssutils.parseString(source)

Expand Down
91 changes: 91 additions & 0 deletions tests/custom_graph.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Customized styles for svg.charts.Graph
*/

.svgBackground{
fill:#222222;
}
.graphBackground{
fill:#343434;
}

/* graphs titles */
.mainTitle{
text-anchor: middle;
fill: #454545;
font-size: %(title_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}
.subTitle{
text-anchor: middle;
fill: #999999;
font-size: %(subtitle_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}

.axis{
stroke: #aaaaaa;
stroke-width: 1px;
}

.guideLines{
stroke: #666666;
stroke-width: 1px;
stroke-dasharray: 5,5;
}

.xAxisLabels{
text-anchor: middle;
fill: #aaaaaa;
font-size: %(x_label_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}

.yAxisLabels{
text-anchor: end;
fill: #aaaaaa;
font-size: %(y_label_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}

.xAxisTitle{
text-anchor: middle;
fill: #ff0000;
font-size: %(x_title_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}

.yAxisTitle{
fill: #ff0000;
text-anchor: middle;
font-size: %(y_title_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}

.dataPointLabel{
fill: #aaaaaa;
text-anchor:middle;
font-size: 10px;
font-family: "Arial", sans-serif;
font-weight: normal;
}

.staggerGuideLine{
fill: none;
stroke: #aaaaaa;
stroke-width: 0.5px;
}

.keyText{
fill: #aaaaaa;
text-anchor:start;
font-size: %(key_font_size)dpx;
font-family: "Arial", sans-serif;
font-weight: normal;
}
17 changes: 17 additions & 0 deletions tests/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def sample_Plot():
return g


def sample_custom_style_Plot():
g = Plot({
'min_x_value': 0,
'min_y_value': 0,
'area_fill': True,
'stagger_x_labels': True,
'stagger_y_labels': True,
'show_x_guidelines': True,
'stylesheet_names': ['custom_graph.css', 'plot.css'],
})
g.add_data({'data': [[1, 25], [2, 30], [3, 45]], 'title': 'series 1'})
g.add_data({'data': [[1, 30], [2, 31], [3, 40]], 'title': 'series 2'})
g.add_data({'data': [[0.5, 35], [1, 20], [3, 10.5]], 'title': 'series 3'})
return g


def sample_PlotTextLabels():
g = Plot({
'draw_lines_between_points': False,
Expand Down Expand Up @@ -68,6 +84,7 @@ def sample_TimeSeries():

def generate_samples():
yield 'Plot', sample_Plot()
yield 'CustomStylePlot', sample_custom_style_Plot()
yield 'PlotTextLabels', sample_PlotTextLabels()
yield 'TimeSeries', sample_TimeSeries()
yield 'VerticalBar', SampleBar.vertical()
Expand Down