@@ -960,6 +960,187 @@ void Demo_MarkersAndText() {
960
960
}
961
961
}
962
962
963
+ void Demo_PlotColors () {
964
+ // Lines
965
+ {
966
+ static float xs1[1001 ], ys1[1001 ];
967
+ ImU32 cs1[1001 ];
968
+ for (int i = 0 ; i < 1001 ; ++i) {
969
+ xs1[i] = i * 0 .001f ;
970
+ ys1[i] = 0 .5f + 0 .5f * sinf (50 * (xs1[i] + (float )ImGui::GetTime () / 10 ));
971
+ ImVec4 lineColor = ImPlot::SampleColormap (ys1[i], ImPlotColormap_Spectral);
972
+ lineColor.w = 1 - powf (1 - (i * 0 .001f ), 4 );
973
+ cs1[i] = ImGui::GetColorU32 (lineColor);
974
+ }
975
+ static double xs2[11 ], ys2[11 ];
976
+ ImU32 cls2[11 ], cms2[11 ];
977
+ for (int i = 0 ; i < 11 ; ++i) {
978
+ xs2[i] = i * 0 .1f ;
979
+ ys2[i] = xs2[i] * xs2[i];
980
+ cls2[i] = ImGui::GetColorU32 (ImPlot::RandomColor ());
981
+ cms2[i] = ImGui::GetColorU32 (ImPlot::RandomColor ());
982
+ }
983
+ if (ImPlot::BeginPlot (" Line Plot with colors" )) {
984
+ ImPlot::SetupAxes (" x" , " f(x)" );
985
+ ImVec4 s1Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Spectral);
986
+ ImPlot::SetNextLineStyle (s1Color);
987
+ ImPlot::SetNextColorsData (ImPlotCol_Line, cs1);
988
+ ImPlot::PlotLine (" sin(x)" , xs1, ys1, 1001 );
989
+ ImVec4 s2Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Twilight);
990
+ ImPlot::SetNextLineStyle (s2Color);
991
+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Circle);
992
+ ImPlot::SetNextColorsData (ImPlotCol_Line, cms2);
993
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerFill, cms2);
994
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, cms2);
995
+ ImPlot::PlotLine (" x^2" , xs2, ys2, 11 );
996
+ ImPlot::EndPlot ();
997
+ }
998
+ }
999
+ // Scatter
1000
+ {
1001
+ srand (0 );
1002
+ static float xs1[100 ], ys1[100 ];
1003
+ static ImU32 cs1[1001 ];
1004
+ for (int i = 0 ; i < 100 ; ++i) {
1005
+ xs1[i] = i * 0 .01f ;
1006
+ ys1[i] = xs1[i] + 0 .1f * ((float )rand () / (float )RAND_MAX);
1007
+ cs1[i] = ImGui::GetColorU32 (ImPlot::SampleColormap (10 * (ys1[i] - xs1[i]), ImPlotColormap_Viridis));
1008
+ }
1009
+ static float xs2[50 ], ys2[50 ];
1010
+ ImU32 cmos2[50 ], cmfs2[50 ];
1011
+
1012
+ for (int i = 0 ; i < 50 ; i++) {
1013
+ xs2[i] = 0 .25f + 0 .2f * ((float )rand () / (float )RAND_MAX);
1014
+ ys2[i] = 0 .75f + 0 .2f * ((float )rand () / (float )RAND_MAX);
1015
+
1016
+ ImVec4 markerOutlineColor = ImPlot::SampleColormap (((float )rand () / (float )RAND_MAX), ImPlotColormap_Plasma);
1017
+ ImVec4 markerFillColor = ImVec4 (markerOutlineColor.x , markerOutlineColor.x , markerOutlineColor.z , 0.5 );
1018
+ cmfs2[i] = ImGui::GetColorU32 (markerFillColor);
1019
+ cmos2[i] = ImGui::GetColorU32 (markerOutlineColor);
1020
+ }
1021
+
1022
+ if (ImPlot::BeginPlot (" Scatter Plot with colors" )) {
1023
+ ImVec4 s1Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Viridis);
1024
+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Circle, 4 , s1Color, IMPLOT_AUTO, s1Color);
1025
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, cs1);
1026
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerFill, cs1);
1027
+ ImPlot::PlotScatter (" Data 1" , xs1, ys1, 100 );
1028
+
1029
+ ImVec4 s2Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Plasma);
1030
+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Square, 6 , s2Color, IMPLOT_AUTO, s2Color);
1031
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, cmos2);
1032
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerFill, cmfs2);
1033
+ ImPlot::PlotScatter (" Data 2" , xs2, ys2, 50 );
1034
+
1035
+ ImPlot::EndPlot ();
1036
+ }
1037
+ }
1038
+
1039
+ // Infinite lines
1040
+ {
1041
+ static double vals[] = { 0.25 , 0.5 , 0.75 };
1042
+ static ImU32 colors[] = { ImGui::GetColorU32 (ImPlot::RandomColor ()), ImGui::GetColorU32 (ImPlot::RandomColor ()), ImGui::GetColorU32 (ImPlot::RandomColor ()) };
1043
+ if (ImPlot::BeginPlot (" ##Infinite with colors" )) {
1044
+ ImPlot::SetupAxes (NULL , NULL , ImPlotAxisFlags_NoInitialFit, ImPlotAxisFlags_NoInitialFit);
1045
+ ImPlot::SetNextLineStyle (ImVec4 (1 , 1 , 1 , 0.5 ));
1046
+ ImPlot::SetNextColorsData (ImPlotCol_Line, colors);
1047
+ ImPlot::PlotInfLines (" VLines" , vals, 3 );
1048
+ ImPlot::SetNextLineStyle (ImVec4 (1 , 1 , 1 , 0.5 ));
1049
+ ImPlot::SetNextColorsData (ImPlotCol_Line, colors);
1050
+ ImPlot::PlotInfLines (" HLines" , vals, 3 , ImPlotInfLinesFlags_Horizontal);
1051
+ ImPlot::EndPlot ();
1052
+ }
1053
+ }
1054
+
1055
+ // Shaded plots
1056
+ {
1057
+ static float xs[1001 ], ys[1001 ], ys1[1001 ], ys2[1001 ], ys3[1001 ], ys4[1001 ];
1058
+ static ImU32 cl1[1001 ];
1059
+ static ImU32 cf1[1001 ];
1060
+ static ImU32 cf2[1001 ];
1061
+ srand (0 );
1062
+
1063
+ for (int i = 0 ; i < 1001 ; ++i) {
1064
+ xs[i] = i * 0 .001f ;
1065
+ ys[i] = 0 .25f + 0 .25f * sinf (25 * xs[i]) * sinf (5 * xs[i]) + RandomRange (-0 .01f , 0 .01f );
1066
+ ys1[i] = ys[i] + RandomRange (0 .1f , 0 .12f );
1067
+ ys2[i] = ys[i] - RandomRange (0 .1f , 0 .12f );
1068
+ ys3[i] = 0 .75f + 0 .2f * sinf (37 * xs[i]);
1069
+ ys4[i] = 0 .75f + 0 .1f * cosf (29 * xs[i]);
1070
+
1071
+ ImVec4 lineColor = ImPlot::SampleColormap (i/1001 .f , ImPlotColormap_Hot);
1072
+ ImVec4 fillColor = lineColor;
1073
+ fillColor.w = 0 .25f ;
1074
+ cl1[i] = ImGui::GetColorU32 (lineColor);
1075
+ cf1[i] = ImGui::GetColorU32 (fillColor);
1076
+
1077
+ float diffNormalized = (float ) fabs (ys3[i] - ys4[i]) / (0 .3f );
1078
+ ImVec4 fillColor2 = ImPlot::SampleColormap (diffNormalized, ImPlotColormap_Plasma);
1079
+ fillColor2.w = 0 .8f ;
1080
+ cf2[i] = ImGui::GetColorU32 (fillColor2);
1081
+ }
1082
+
1083
+ if (ImPlot::BeginPlot (" Shaded Plots with colors" )) {
1084
+ ImPlot::SetNextColorsData (ImPlotCol_Fill, cf1);
1085
+ ImPlot::PlotShaded (" Uncertain Data" , xs, ys1, ys2, 1001 );
1086
+ ImPlot::SetNextColorsData (ImPlotCol_Line, cl1);
1087
+ ImPlot::PlotLine (" Uncertain Data" , xs, ys, 1001 );
1088
+ ImPlot::SetNextColorsData (ImPlotCol_Fill, cf2);
1089
+ ImPlot::PlotShaded (" Overlapping" , xs, ys3, ys4, 1001 );
1090
+ ImPlot::PlotLine (" Overlapping" , xs, ys3, 1001 );
1091
+ ImPlot::PlotLine (" Overlapping" , xs, ys4, 1001 );
1092
+ ImPlot::EndPlot ();
1093
+ }
1094
+ }
1095
+
1096
+ // Stairstep
1097
+ {
1098
+ static float ys1[21 ], ys2[21 ], s;
1099
+ static ImU32 clys1[21 ], cfys1[21 ], clys2[21 ], cfys2[21 ];
1100
+ for (int i = 0 ; i < 21 ; ++i) {
1101
+ ys1[i] = 0 .75f + 0 .2f * sinf (10 * i * 0 .05f );
1102
+ ys2[i] = 0 .25f + 0 .2f * sinf (10 * i * 0 .05f );
1103
+ ImVec4 cl1 = ImPlot::RandomColor ();
1104
+ ImVec4 cl2 = ImPlot::RandomColor ();
1105
+ ImVec4 cf1 = cl1;
1106
+ ImVec4 cf2 = cl2;
1107
+ cf1.w = 0.5 ;
1108
+ cf2.w = 0.5 ;
1109
+
1110
+ clys1[i] = ImGui::GetColorU32 (cl1);
1111
+ clys2[i] = ImGui::GetColorU32 (cl2);
1112
+ cfys1[i] = ImGui::GetColorU32 (cf1);
1113
+ cfys2[i] = ImGui::GetColorU32 (cf2);
1114
+ }
1115
+ static ImPlotStairsFlags flags = 0 ;
1116
+ CHECKBOX_FLAG (flags, ImPlotStairsFlags_Shaded);
1117
+ if (ImPlot::BeginPlot (" Stairstep Plot with colors" )) {
1118
+ ImPlot::SetupAxes (" x" , " f(x)" );
1119
+ ImPlot::SetupAxesLimits (0 , 1 , 0 , 1 );
1120
+
1121
+ ImPlot::PushStyleColor (ImPlotCol_Line, ImVec4 (0 .5f , 0 .5f , 0 .5f , 1 .0f ));
1122
+ ImPlot::PlotLine (" ##1" , ys1, 21 , 0 .05f );
1123
+ ImPlot::PlotLine (" ##2" , ys2, 21 , 0 .05f );
1124
+ ImPlot::PopStyleColor ();
1125
+
1126
+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Circle);
1127
+ ImPlot::SetNextFillStyle (IMPLOT_AUTO_COL, 0 .25f );
1128
+ ImPlot::SetNextColorsData (ImPlotCol_Line, clys1);
1129
+ ImPlot::SetNextColorsData (ImPlotCol_Fill, cfys1);
1130
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, clys1);
1131
+ ImPlot::PlotStairs (" Post Step (default)" , ys1, 21 , 0 .05f , 0 , flags);
1132
+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Circle);
1133
+ ImPlot::SetNextFillStyle (IMPLOT_AUTO_COL, 0 .25f );
1134
+ ImPlot::SetNextColorsData (ImPlotCol_Line, clys2);
1135
+ ImPlot::SetNextColorsData (ImPlotCol_Fill, cfys2);
1136
+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, clys2);
1137
+ ImPlot::PlotStairs (" Pre Step" , ys2, 21 , 0 .05f , 0 , flags | ImPlotStairsFlags_PreStep);
1138
+
1139
+ ImPlot::EndPlot ();
1140
+ }
1141
+ }
1142
+ }
1143
+
963
1144
// -----------------------------------------------------------------------------
964
1145
965
1146
void Demo_NaNValues () {
@@ -2214,6 +2395,7 @@ void ShowDemoWindow(bool* p_open) {
2214
2395
DemoHeader (" Images" , Demo_Images);
2215
2396
DemoHeader (" Markers and Text" , Demo_MarkersAndText);
2216
2397
DemoHeader (" NaN Values" , Demo_NaNValues);
2398
+ DemoHeader (" Plot Colors" , Demo_PlotColors);
2217
2399
ImGui::EndTabItem ();
2218
2400
}
2219
2401
if (ImGui::BeginTabItem (" Subplots" )) {
0 commit comments