Skip to content

Commit 452e7be

Browse files
committed
added: demo_SymmetricLogScale()
added: demo_NaNValues()
1 parent f69bee5 commit 452e7be

File tree

5 files changed

+150
-36
lines changed

5 files changed

+150
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3535

36-
# ImGuinZ x
36+
# ImGuinZ
3737

3838
This project aims to simply and easily build [Dear ImGui](https://github.com/ocornut/imgui) examples with **Zig language** using [CImGui](https://github.com/cimgui/cimgui) / [CImPlot](https://github.com/cimgui/cimplot) as first step.
3939

examples/imPlotDemo/imgui.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Size=444,364
4444
Collapsed=1
4545

4646
[Window][ImPlot Demo]
47-
Pos=528,9
47+
Pos=578,11
4848
Size=600,750
4949
Collapsed=0
5050

@@ -79,8 +79,8 @@ Size=568,552
7979
Collapsed=0
8080

8181
[Window][ ImPlot demo: All demos have been written in Zig lang.]
82-
Pos=21,16
83-
Size=531,722
82+
Pos=17,13
83+
Size=546,722
8484
Collapsed=0
8585

8686
[Table][0x53416836,3]

examples/imPlotDemo/src/demoAll.zig

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//
2+
// 2024/07: Converted to Zig lang. from C++ by dinau
3+
//
14
const std = @import("std");
25
const math = @import("std").math;
36
//
@@ -11,7 +14,9 @@ pub const c = @cImport({
1114
@cInclude("stdlib.h");
1215
});
1316

14-
17+
//----------------
18+
// imPlotDemoTabs
19+
//----------------
1520
pub fn imPlotDemoTabs() !void {
1621
if (ig.igBeginTabBar("ImPlotDemoTabs", 0)) {
1722
defer ig.igEndTabBar();
@@ -36,19 +41,19 @@ pub fn imPlotDemoTabs() !void {
3641
// demoHeader("Digital Plots", demo_DigitalPlots);
3742
try demoHeader("Images", demo_Images);
3843
try demoHeader("Markers and Text", demo_MarkersAndText);
39-
// demoHeader("NaN Values", demo_NaNValues);
44+
try demoHeader("NaN Values", demo_NaNValues);
4045
}
4146
if (ig.igBeginTabItem(fonts.ICON_FA_CHART_AREA ++ " Subplots", null, 0)) {
4247
defer ig.igEndTabItem();
43-
// demoHeader("Sizing", demo_SubplotsSizing);
48+
try demoHeader("Sizing", demo_SubplotsSizing);
4449
// demoHeader("Item Sharing", demo_SubplotItemSharing);
4550
// demoHeader("Axis Linking", demo_SubplotAxisLinking);
4651
try demoHeader("Tables", demo_Tables);
4752
}
4853
if (ig.igBeginTabItem(fonts.ICON_FA_CHART_COLUMN ++ " Axes", null, 0)) {
4954
defer ig.igEndTabItem();
5055
try demoHeader("Log Scale", demo_LogScale);
51-
// demoHeader("Symmetric Log Scale", demo_SymmetricLogScale);
56+
try demoHeader("Symmetric Log Scale", demo_SymmetricLogScale);
5257
// demoHeader("Time Scale", demo_TimeScale);
5358
// demoHeader("Custom Scale", demo_CustomScale);
5459
// demoHeader("Multiple Axes", demo_MultipleAxes);
@@ -96,6 +101,55 @@ fn demoHeader(label: anytype, demo: anytype) !void {
96101
}
97102
}
98103

104+
//-----------------------
105+
// demo_SubplotsSizing()
106+
//-----------------------
107+
pub fn demo_SubplotsSizing() !void {
108+
if(false){
109+
const st = struct {
110+
var flags = ip.ImPlotSubplotFlags_ShareItems | ip.ImPlotSubplotFlags_NoLegend;
111+
var rows:c_int = 3;
112+
var cols:c_int = 3;
113+
var rratios = [_]f32{5,1,1,1,1,1};
114+
var cratios = [_]f32{5,1,1,1,1,1};
115+
};
116+
_ = ig.igCheckboxFlags_IntPtr("ImPlotSubplotFlags_NoResize", &st.flags, ip.ImPlotSubplotFlags_NoResize);
117+
_ = ig.igCheckboxFlags_IntPtr("ImPlotSubplotFlags_NoTitle", &st.flags, ip.ImPlotSubplotFlags_NoTitle);
118+
119+
_ = ig.igSliderInt("Rows",&st.rows,1,5, "%d", 0);
120+
_ = ig.igSliderInt("Cols",&st.cols,1,5, "%d", 0);
121+
if ( (st.rows < 1) or (st.cols < 1)) {
122+
ig.igTextColored(.{.x=1, .y=0, .z=0, .w=1}, "Nice try, but the number of rows and columns must be greater than 0!");
123+
return;
124+
}
125+
_ = ig.igDragScalarN("Row Ratios",ig.ImGuiDataType_Float,&st.rratios,st.rows,0.01,null,null,null,0);
126+
_ = ig.igDragScalarN("Col Ratios",ig.ImGuiDataType_Float,&st.cratios,st.cols,0.01,null,null,null,0);
127+
if (ip.ImPlot_BeginSubplots("My Subplots", st.rows, st.cols, .{.x=-1, .y=400}, st.flags, &st.rratios, &st.cratios)) {
128+
var id:c_int = 0;
129+
for (0..@intCast((st.rows * st.cols)))|i| {
130+
if (ip.ImPlot_BeginPlot("", .{.x = 0, .y = 0}, ip.ImPlotFlags_NoLegend)) {
131+
ip.ImPlot_SetupAxes(null,null,ip.ImPlotAxisFlags_NoDecorations,ip.ImPlotAxisFlags_NoDecorations);
132+
var fi = 0.01 * (@as(f32,@floatFromInt(i)) + 1);
133+
if (st.rows*st.cols > 1) {
134+
// TODO
135+
var vec4 :ig.ImVec4 = undefined;
136+
ip.ImPlot_SampleColormap(@ptrCast(&vec4), @as(f32,@floatFromInt(i)) / @as(f32,@floatFromInt((st.rows * st.cols - 1))), ip.ImPlotColormap_Jet);
137+
ip.ImPlot_SetNextLineStyle(.{.x = vec4.x, .y = vec4.y, .z = vec4.z, .w = vec4.w}
138+
, utils.IMPLOT_AUTO);
139+
}
140+
//var label:[16]u8 = undefined;
141+
//const slLabel = try std.fmt.bufPrint(&label, "data{}", id);
142+
id += 1;
143+
//ip.ImPlot_PlotLineG(slLabel.ptr, utils.SineWave, &fi,1000, 0);
144+
ip.ImPlot_PlotLineG("mmmm", utils.SineWave, &fi,1000, 0);
145+
ip.ImPlot_EndPlot();
146+
}
147+
}
148+
ip.ImPlot_EndSubplots();
149+
}
150+
}
151+
}
152+
99153
//---------------
100154
// demo_LogScale()
101155
//---------------
@@ -121,7 +175,29 @@ pub fn demo_LogScale() !void {
121175
}
122176
}
123177

178+
//-----------------
179+
// demo_LogScale()
180+
//-----------------
181+
pub fn demo_SymmetricLogScale() !void {
182+
var xs:[1001]f64 = undefined;
183+
var ys1:[1001]f64 = undefined;
184+
var ys2:[1001]f64 = undefined;
185+
for (0..1001)|i| {
186+
xs[i] = @as(f64,@floatFromInt(i)) * 0.1 - 50;
187+
ys1[i] = math.sin(xs[i]);
188+
ys2[i] = @as(f64,@floatFromInt(i)) * 0.002 - 1;
189+
}
190+
if (ip.ImPlot_BeginPlot("SymLog Plot", .{.x=-1, .y=0} ,0)) {
191+
ip.ImPlot_SetupAxisScale_PlotScale(ip.ImAxis_X1, ip.ImPlotScale_SymLog);
192+
try ip.ImPlot_PlotLineXy("f(x) = a*x+b" ,&xs, &ys2,1001);
193+
try ip.ImPlot_PlotLineXy("f(x) = math.sin(x)" ,&xs, &ys1,1001);
194+
ip.ImPlot_EndPlot();
195+
}
196+
}
124197

198+
//-----------------
199+
// demo_Config()
200+
//-----------------
125201
pub fn demo_Config() !void {
126202
ig.igShowFontSelector("Font");
127203
_ = ig.igShowStyleSelector("ImGui Style");
@@ -584,6 +660,34 @@ pub fn demo_MarkersAndText() !void {
584660
}
585661
}
586662

663+
//------------------
664+
// demo_NaNValues()
665+
//------------------
666+
pub fn demo_NaNValues() !void {
667+
const st = struct {
668+
var include_nan = true;
669+
var flags:ip.ImPlotLineFlags = 0;
670+
};
671+
672+
var data1 = [5]f32{0.0,0.25,0.5,0.75,1.0};
673+
var data2 = [5]f32{0.0,0.25,0.5,0.75,1.0};
674+
675+
if (st.include_nan){
676+
data1[2] = utils.NaN_f32;
677+
}
678+
679+
_ = ig.igCheckbox("Include NaN",&st.include_nan);
680+
ig.igSameLine(0, -1.0);
681+
_ = ig.igCheckboxFlags_IntPtr("Skip NaN", &st.flags, ip.ImPlotLineFlags_SkipNaN);
682+
683+
if (ip.ImPlot_BeginPlot("##NaNValues", .{ .x = -1, .y = 0 }, 0)) {
684+
ip.ImPlot_SetNextMarkerStyle(ip.ImPlotMarker_Square, utils.IMPLOT_AUTO, utils.IMPLOT_AUTO_COL, utils.IMPLOT_AUTO, utils.IMPLOT_AUTO_COL);
685+
try ip.ImPlot_PlotLineXyEx("line", &data1, &data2, 5, st.flags, 0, utils.stride(data1[0]));
686+
try ip.ImPlot_PlotBars("bars", &data1, 5);
687+
ip.ImPlot_EndPlot();
688+
}
689+
}
690+
587691
//------------------
588692
// demo_LinePlots()
589693
//------------------

examples/imPlotDemo/src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn glfw_error_callback (err: c_int, description: [*c] const u8) callconv (.C) vo
1616
std.debug.print ("GLFW Error {d}: {s}\n", .{ err, description });
1717
}
1818

19-
const MainWinWidth :i32 = 1100;
19+
const MainWinWidth :i32 = 1200;
2020
const MainWinHeight:i32 = 800;
2121

2222
//--------

examples/imPlotDemo/src/utils.zig

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ pub const c = @cImport ({
55
@cInclude ("stdlib.h");
66
});
77

8-
pub const ig = @cImport ({
9-
@cInclude ("cimgui.h");
10-
@cInclude ("cimplot.h");
11-
});
12-
const ip = @import("zimplot.zig");
8+
const ig = @import ("imgui.zig");
9+
const ip = @import("implot.zig");
1310

1411
pub const IMPLOT_AUTO: f32 = -1;
1512
pub const IMPLOT_AUTO_COL = .{.x = 0, .y = 0, .z = 0, .w = -1};
1613
pub const INFINITY_f32 = std.math.inf(f32);
1714
pub const INFINITY_f64 = std.math.inf(f64);
15+
pub const NaN_f32 = std.math.nan(f32);
16+
pub const NaN_f64 = std.math.nan(f64);
1817
pub fn stride(value:anytype) c_int {
1918
return @sizeOf(@TypeOf(value));
2019
}
@@ -46,22 +45,33 @@ pub const WaveData = struct {
4645
}
4746
};
4847

49-
pub fn SineWave(idx: u32, wave_data: *WaveData) ImPlotPoint {
50-
const t = idx;
51-
return ImPlotPoint{
52-
.x = wave_data.x + t,
53-
.y = wave_data.amp * math.sin(wave_data.freq * (wave_data.x + t) + wave_data.offset),
54-
};
48+
pub fn SineWave(data: ?*anyopaque, idx: c_int, point: [*c]ip.ImPlotPoint ) callconv(.C) ?*anyopaque {
49+
const v1:*f32 = @as(*f32,@alignCast(data.?));
50+
const fval = v1.*;
51+
const fidx = @as(f32,@floatFromInt(idx));
52+
point.x = fval;
53+
point.y = math.sin(fval * fidx );
54+
return point;
5555
}
56-
57-
pub fn SawWave(idx: u32, wave_data: *WaveData) ImPlotPoint {
58-
const t = idx;
59-
return ImPlotPoint{
60-
.x = wave_data.x + t,
61-
.y = wave_data.amp * 2.0 * (wave_data.freq * (wave_data.x + t) - math.floor(0.5 + wave_data.freq * (wave_data.x + t))),
62-
};
56+
// Nim: ///type ImPlotPointGetter* = proc (data: pointer; idx: cint; point: ptr ImPlotPoint): pointer {.cdecl.}
57+
// C++: typedef ImPlotPoint (*ImPlotGetter) ( int idx, void* user_data);
58+
// void PlotLineG (const char* label_id, ImPlotGetter getter, void* data, int count, ImPlotLineFlags flags=0);
59+
// C: typedef void * (*ImPlotPoint_getter)(void* data, int idx, ImPlotPoint *point);
60+
// void ImPlot_PlotLineG(const char* label_id, ImPlotPoint_getter getter,void* data, int count, ImPlotLineFlags flags);//custom generation
61+
//
62+
pub fn SinewaveGetter(i:c_int, data:*anyopaque) ImPlotPoint {
63+
const f = (@as(f32,@ptrCast(data))).*;
64+
return ImPlotPoint(i,math.sin(f * i));
6365
}
6466

67+
//pub fn SawWave(data: *anyopaque, idx: u32, point: ip.ImPlotPoint) *anyopaque {
68+
// const t = idx;
69+
// return ImPlotPoint{
70+
// .x = wave_data.x + t,
71+
// .y = wave_data.amp * 2.0 * (wave_data.freq * (wave_data.x + t) - math.floor(0.5 + wave_data.freq * (wave_data.x + t))),
72+
// };
73+
//}
74+
6575
pub fn Spiral(idx: u32, wave_data: *WaveData) ImPlotPoint {
6676
const t = idx;
6777
return ImPlotPoint{
@@ -238,16 +248,16 @@ pub const HugeTimeData = struct {
238248
// // Implement sparkline plotting
239249
//}
240250
pub fn Sparkline(id: anytype, values: anytype, count: c_int, min_v: f32, max_v: f32, offset: c_int, col: anytype, size: ig.ImVec2) void {
241-
ig.ImPlot_PushStyleVar_Vec2(ig.ImPlotStyleVar_PlotPadding, .{ .x = 0, .y = 0 });
242-
if (ig.ImPlot_BeginPlot(id, size, ig.ImPlotFlags_CanvasOnly)) {
243-
ig.ImPlot_SetupAxes(null, null, ig.ImPlotAxisFlags_NoDecorations, ig.ImPlotAxisFlags_NoDecorations);
244-
ig.ImPlot_SetupAxesLimits(0, @floatFromInt(count - 1), min_v, max_v, ig.ImGuiCond_Always);
245-
ig.ImPlot_SetNextLineStyle(col, IMPLOT_AUTO);
246-
ig.ImPlot_SetNextFillStyle(col, 0.25);
247-
try ip.ImPlot_PlotLineEx(id, values, count, 1.0, 0, ig.ImPlotLineFlags_Shaded, offset, stride(values[0]));
248-
ig.ImPlot_EndPlot();
251+
ip.ImPlot_PushStyleVar_Vec2(ip.ImPlotStyleVar_PlotPadding, .{ .x = 0, .y = 0 });
252+
if (ip.ImPlot_BeginPlot(id, .{.x=size.x,.y=size.y}, ip.ImPlotFlags_CanvasOnly)) {
253+
ip.ImPlot_SetupAxes(null, null, ip.ImPlotAxisFlags_NoDecorations, ip.ImPlotAxisFlags_NoDecorations);
254+
ip.ImPlot_SetupAxesLimits(0, @floatFromInt(count - 1), min_v, max_v, ig.ImGuiCond_Always);
255+
ip.ImPlot_SetNextLineStyle(col, IMPLOT_AUTO);
256+
ip.ImPlot_SetNextFillStyle(col, 0.25);
257+
try ip.ImPlot_PlotLineEx(id, values, count, 1.0, 0, ip.ImPlotLineFlags_Shaded, offset, stride(values[0]));
258+
ip.ImPlot_EndPlot();
249259
}
250-
ig.ImPlot_PopStyleVar(1);
260+
ip.ImPlot_PopStyleVar(1);
251261
}
252262
//
253263
//pub fn PlotCandlestick(label_id: []const u8, xs: []const f64, opens: []const f64, closes: []const f64, lows: []const f64, highs: []const f64, count: i32, tooltip: bool = true, width_percent: f32 = 0.25, bullCol: ImVec4 = ImVec4{.x = 0, .y = 1, .z = 0, .w = 1}, bearCol: ImVec4 = ImVec4{.x = 1, .y = 0, .z = 0, .w = 1}) void {

0 commit comments

Comments
 (0)