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

[will be dropped]From my own purpose. #32

Open
wants to merge 12 commits into
base: master
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,24 @@ chart.add_unaware_timestamp_collection_by_index(0, unaware_timestamp_collection,

Et voilà !

## CAUTIONS

### Removing LiveChart.Chart from Gtk.Widget

Removing LiveChart.Chart from Gtk.Widget without stopping auto-refresh causes memory leak.

```vala
var window = new Gtk.Window();
var chart = new LiveChart.Chart();
window.add(chart);

//...
chart.refresh_every(-1); //Don't forget to stop auto-refresh if your app replaces the LiveChart.Chart widget.
window.remove(chart);

```


## How LiveChart versions works ?

* For each new feature, the `minor` version number will be bumped
Expand Down
7 changes: 6 additions & 1 deletion examples/live-chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public class Example : Gtk.Window {
heat.add(heat_value);
return true;
});


Timeout.add(20000, () => {
chart.remove_serie(rss);
return false;
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be a nice if we could have a button to remove that serie instead of waiting 20s

var export_button = new Gtk.Button.with_label("Export to PNG");
export_button.clicked.connect (() => {
try {
Expand Down
18 changes: 15 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
project('live-chart', ['vala', 'c'], version: '1.9.1')
project('live-chart', ['vala', 'c'], version: '1.9.1-RO')

cc = meson.get_compiler('c')
libm = cc.find_library('m', required: true)

gtk = dependency('gtk+-3.0', version: '>= 3.22')
#setting up GEE
gee = dependency('gee-0.8')

vala_args = ['--target-glib=2.50']

#setting up gtk
gtk_major = 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GTK version should define in meson_options.txt:

option('GTK',
       type: 'integer',
       value: 4,
       description : 'GTK version')

then use it in the meson:

if 3 == get_option('GTK')
else
endif

then change the GTK version by command:
meson -DGTK=3 build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.
I tried it, and it works.
I'll take it in my repository later.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, as talked in #37, the default build should be GTK3 for now if we don't provide any argument to meson.


if gtk_major == 3
gtk = dependency('gtk+-3.0', version: '>= 3.22')
vala_args += ['--define=GTK3']
endif

if gtk_major == 4
gtk = dependency('gtk4')
vala_args += ['--define=GTK4']
endif

if meson.version().version_compare('>= 0.47')
if get_option('debug') == true
vala_args += ['--ccode', '--debug']
Expand Down
8 changes: 4 additions & 4 deletions src/axis.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace LiveChart {
public Path lines = new Path();

public XAxis() {
axis.color = {0.5, 0.5, 0.5, 0.5};
lines.color = {0.5, 0.5, 0.5, 0.2};
axis.color = {0.5f, 0.5f, 0.5f, 0.5f};
lines.color = {0.5f, 0.5f, 0.5f, 0.2f};
}

public double get_ratio() {
Expand Down Expand Up @@ -51,8 +51,8 @@ namespace LiveChart {
public YAxis(string unit = "") {
this.unit = unit;
ticks = get_ticks();
axis.color = {0.5, 0.5, 0.5, 0.5};
lines.color = {0.5, 0.5, 0.5, 0.2};
axis.color = {0.5f, 0.5f, 0.5f, 0.5f};
lines.color = {0.5f, 0.5f, 0.5f, 0.2f};
bounds.notify["upper"].connect(() => {
this.ticks = get_ticks();
});
Expand Down
8 changes: 4 additions & 4 deletions src/background.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace LiveChart {
[Version (deprecated = true, deprecated_since = "1.8.0", replacement = "Background.color")]
public Gdk.RGBA main_color {
get; set; default= Gdk.RGBA() {
red = 0.1,
green = 0.1,
blue = 0.1,
alpha = 1.0
red = 0.1f,
green = 0.1f,
blue = 0.1f,
alpha = 1.0f
};
}

Expand Down
51 changes: 42 additions & 9 deletions src/chart.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,48 @@ namespace LiveChart {
public Series series;

private uint source_timeout = 0;

private double play_ratio = 1.0;

private int64 prev_time;

public Chart(Config config = new Config()) {
this.config = config;

#if GTK3
this.size_allocate.connect((allocation) => {
this.config.height = allocation.height;
this.config.width = allocation.width;
});

this.draw.connect(render);

#endif
#if GTK4
this.set_draw_func((_, ctx, width, height) => {
this.config.height = height;
this.config.width = width;
this.render(_, ctx);
});
#endif
this.refresh_every(100);

series = new Series(this);
this.destroy.connect(() => {
refresh_every(-1);
remove_all_series();
});
}

public void add_serie(Serie serie) {
this.series.register(serie);
}

public void remove_serie(Serie serie){
this.series.remove_serie(serie);
}

public void remove_all_series(){
this.series.remove_all();
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That kind of API improvements should be added to README.

[Version (deprecated = true, deprecated_since = "1.7.0", replacement = "Retrieve the Serie from Chart.series (or from the serie you created) and add the value using serie.add")]
public void add_value(Serie serie, double value) {
serie.add(value);
Expand Down Expand Up @@ -72,26 +95,36 @@ namespace LiveChart {
}

public void to_png(string filename) throws Error {
#if GTK3
var window = this.get_window();
if (window == null) {
throw new ChartError.EXPORT_ERROR("Chart is not realized yet");
}
var pixbuff = Gdk.pixbuf_get_from_window(window, 0, 0, window.get_width(), window.get_height());
pixbuff.savev(filename, "png", {}, {});
#endif
}

public void refresh_every(int ms) {
public void refresh_every(int ms, double play_ratio = 1.0) {
this.play_ratio = play_ratio;
if (source_timeout != 0) {
GLib.Source.remove(source_timeout);
source_timeout = 0;
}
if(ms > 0){
this.prev_time = GLib.get_monotonic_time() / 1000;
source_timeout = Timeout.add(ms, () => {
var now = GLib.get_monotonic_time() / 1000;
config.time.current += (int64)((now - this.prev_time) * this.play_ratio);
this.prev_time = now;
this.queue_draw();
return true;
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation is welcome :)

}
source_timeout = Timeout.add(ms, () => {
this.queue_draw();
return true;
});
}

private bool render(Gtk.Widget _, Context ctx) {

ctx.set_antialias(Cairo.Antialias.NONE);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we loose render quality if we disable antialias ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to forgot to revert it. It's my mistake.

config.configure(ctx, legend);

this.background.draw(ctx, config);
Expand Down
32 changes: 29 additions & 3 deletions src/config.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,28 @@ namespace LiveChart {
public int width;
public int height;
}



public class Config {

private int _width = 0;
public int width {
get; set; default = 0;
get{
return _width;
}
set{
if(_width != value){
//i = config.width - config.padding.right; i > config.padding.left; i -= config.x_axis.tick_length
if(x_axis.tick_length <= 0.0){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's commented, it's certainly useless :)

time.head_offset = -1.0;
}
else{
var tmp = value / x_axis.tick_length;
time.head_offset = tmp * x_axis.tick_interval * 1000.0;
}
}
_width = value;
}
}

public int height {
Expand All @@ -53,7 +70,16 @@ namespace LiveChart {

public YAxis y_axis = new YAxis();
public XAxis x_axis = new XAxis();


public struct TimeSeek {
int64 current;
double head_offset;
}
public TimeSeek time = {
GLib.get_real_time() / 1000,
0
};

internal Gee.ArrayList<string> categories;

public Boundaries boundaries() {
Expand Down
2 changes: 1 addition & 1 deletion src/font.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LiveChart {
face = "Sans serif";
slant = FontSlant.NORMAL;
weight = FontWeight.NORMAL;
color = {0.4, 0.4, 0.4, 1.0};
color = {0.4f, 0.4f, 0.4f, 1.0f};
}

public void configure(Context ctx) {
Expand Down
11 changes: 6 additions & 5 deletions src/grid.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace LiveChart {
public bool visible { get; set; default = true; }
public Gdk.RGBA main_color {
get; set; default= Gdk.RGBA() {
red = 0.4,
green = 0.4,
blue = 0.4,
alpha = 1.0
red = 0.4f,
green = 0.4f,
blue = 0.4f,
alpha = 1.0f
};
}

Expand Down Expand Up @@ -67,7 +67,8 @@ namespace LiveChart {
}

protected void render_vgrid(Context ctx, Config config) {
var time = new DateTime.now().to_unix();
// var time = new DateTime.now().to_unix();
var time = config.time.current / 1000;
for (double i = config.width - config.padding.right; i > config.padding.left; i -= config.x_axis.tick_length) {
if (config.x_axis.lines.visible) {
config.x_axis.lines.configure(ctx);
Expand Down
21 changes: 15 additions & 6 deletions src/legend.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ namespace LiveChart {
height=0
};
public Gdk.RGBA main_color {
get; set; default= Gdk.RGBA() {
red = 1.0,
green = 1.0,
blue = 1.0,
alpha = 1.0
get; set;
default= Gdk.RGBA() {
red = 1.0f,
green = 1.0f,
blue = 1.0f,
alpha = 1.0f
};
}
public void add_legend(Serie serie) {
series.add(serie);
}

public void remove_legend(Serie serie){
if(series.contains(serie)){
series.remove(serie);
}
}
public void remove_all_legend(){
series.clear();
}

public abstract void draw(Context ctx, Config config);
public BoundingBox get_bounding_box() {
return bounding_box;
Expand Down
2 changes: 1 addition & 1 deletion src/path.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace LiveChart {
public Gdk.RGBA color { get; set; }
public bool visible {get; set; }

public Path(double width = 0.5, Gdk.RGBA color = {1.0, 1.0, 1.0, 1.0}, bool visible = true, Dash? dash = null) {
public Path(double width = 0.5, Gdk.RGBA color = {1.0f, 1.0f, 1.0f, 1.0f}, bool visible = true, Dash? dash = null) {
this.width = width;
this.color = color;
this.visible = true;
Expand Down
24 changes: 20 additions & 4 deletions src/points.vala
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ namespace LiveChart {
var boundaries = config.boundaries();

Points points = new Points();
if (values.size > 0) {
var last_value = values.last();
points.realtime_delta = (((GLib.get_real_time() / 1000) - last_value.timestamp) * config.x_axis.get_ratio()) / 1000;
if (values.size > 1) {
/// \note SortedSet<G>.sub_set won't work as I expected correctly.
SortedSet<TimestampedValue?> renderee = null;
TimestampedValue border = {(double)config.time.current + 1, 0.0};
renderee = values.head_set(border);

foreach (TimestampedValue value in values) {
if(config.time.head_offset >= 0.0 && renderee.size > 0){
border.timestamp -= config.time.head_offset;
if(renderee.first().timestamp < border.timestamp){
renderee = renderee.tail_set(border);
}
}

//var renderee = values;
if(renderee.size <= 2){
return points;
}
var last_value = renderee.last();
//points.realtime_delta = (((GLib.get_real_time() / 1000) - last_value.timestamp) * config.x_axis.get_ratio()) / 1000;
points.realtime_delta = ((config.time.current - last_value.timestamp) * config.x_axis.get_ratio()) / 1000;
foreach (TimestampedValue value in renderee) {
var point = Points.value_to_point(last_value, value, config, boundaries, points.realtime_delta);
points.add(point);
}
Expand Down
Loading