@@ -926,6 +926,99 @@ void ShowDemo_MarkersAndText() {
926926 }
927927}
928928
929+ void ShowDemo_PlotColors () {
930+ // Lines
931+ {
932+ static float xs1[1001 ], ys1[1001 ];
933+ ImU32 cs1[1001 ];
934+ for (int i = 0 ; i < 1001 ; ++i) {
935+ xs1[i] = i * 0 .001f ;
936+ ys1[i] = 0 .5f + 0 .5f * sinf (50 * (xs1[i] + (float )ImGui::GetTime () / 10 ));
937+ ImVec4 lineColor = ImPlot::SampleColormap (ys1[i], ImPlotColormap_Spectral);
938+ lineColor.w = 1 - powf (1 - (i * 0 .001f ), 4 );
939+ cs1[i] = ImGui::GetColorU32 (lineColor);
940+ }
941+ static double xs2[11 ], ys2[11 ];
942+ ImU32 cls2[11 ], cms2[11 ];
943+ for (int i = 0 ; i < 11 ; ++i) {
944+ xs2[i] = i * 0 .1f ;
945+ ys2[i] = xs2[i] * xs2[i];
946+ cls2[i] = ImGui::GetColorU32 (ImPlot::SampleColormap (float (ys2[i]), ImPlotColormap_Twilight));
947+ cms2[i] = ImGui::GetColorU32 (ImPlot::RandomColor ());
948+ }
949+ if (ImPlot::BeginPlot (" Line Plot with colors" )) {
950+ ImPlot::SetupAxes (" x" , " f(x)" );
951+ ImVec4 s1Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Spectral);
952+ ImPlot::SetNextLineStyle (s1Color);
953+ ImPlot::SetNextColorsData (ImPlotCol_Line, cs1);
954+ ImPlot::PlotLine (" sin(x)" , xs1, ys1, 1001 );
955+ ImVec4 s2Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Twilight);
956+ ImPlot::SetNextLineStyle (s2Color);
957+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Circle);
958+ ImPlot::SetNextColorsData (ImPlotCol_Line, cls2);
959+ ImPlot::SetNextColorsData (ImPlotCol_MarkerFill, cms2);
960+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, cms2);
961+ ImPlot::PlotLine (" x^2" , xs2, ys2, 11 );
962+ ImPlot::EndPlot ();
963+ }
964+ }
965+ // Scatter
966+ {
967+ srand (0 );
968+ static float xs1[100 ], ys1[100 ];
969+ static ImU32 cs1[1001 ];
970+ for (int i = 0 ; i < 100 ; ++i) {
971+ xs1[i] = i * 0 .01f ;
972+ ys1[i] = xs1[i] + 0 .1f * ((float )rand () / (float )RAND_MAX);
973+ cs1[i] = ImGui::GetColorU32 (ImPlot::SampleColormap (10 * (ys1[i] - xs1[i]), ImPlotColormap_Viridis));
974+ }
975+ static float xs2[50 ], ys2[50 ];
976+ ImU32 cmos2[50 ], cmfs2[50 ];
977+
978+ for (int i = 0 ; i < 50 ; i++) {
979+ xs2[i] = 0 .25f + 0 .2f * ((float )rand () / (float )RAND_MAX);
980+ ys2[i] = 0 .75f + 0 .2f * ((float )rand () / (float )RAND_MAX);
981+
982+ ImVec4 markerOutlineColor = ImPlot::SampleColormap (((float )rand () / (float )RAND_MAX), ImPlotColormap_Plasma);
983+ ImVec4 markerFillColor = ImVec4 (markerOutlineColor.x , markerOutlineColor.x , markerOutlineColor.z , 0.5 );
984+ cmfs2[i] = ImGui::GetColorU32 (markerFillColor);
985+ cmos2[i] = ImGui::GetColorU32 (markerOutlineColor);
986+ }
987+
988+ if (ImPlot::BeginPlot (" Scatter Plot with colors" )) {
989+ ImVec4 s1Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Viridis);
990+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Circle, 4 , s1Color, IMPLOT_AUTO, s1Color);
991+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, cs1);
992+ ImPlot::SetNextColorsData (ImPlotCol_MarkerFill, cs1);
993+ ImPlot::PlotScatter (" Data 1" , xs1, ys1, 100 );
994+
995+ ImVec4 s2Color = ImPlot::SampleColormap (0 .5f , ImPlotColormap_Plasma);
996+ ImPlot::SetNextMarkerStyle (ImPlotMarker_Square, 6 , s2Color, IMPLOT_AUTO, s2Color);
997+ ImPlot::SetNextColorsData (ImPlotCol_MarkerOutline, cmos2);
998+ ImPlot::SetNextColorsData (ImPlotCol_MarkerFill, cmfs2);
999+ ImPlot::PlotScatter (" Data 2" , xs2, ys2, 50 );
1000+
1001+ ImPlot::EndPlot ();
1002+ }
1003+ }
1004+
1005+ // Infinite lines
1006+ {
1007+ static double vals[] = { 0.25 , 0.5 , 0.75 };
1008+ static ImU32 colors[] = { ImGui::GetColorU32 (ImPlot::RandomColor ()), ImGui::GetColorU32 (ImPlot::RandomColor ()), ImGui::GetColorU32 (ImPlot::RandomColor ()) };
1009+ if (ImPlot::BeginPlot (" ##Infinite with colors" )) {
1010+ ImPlot::SetupAxes (NULL , NULL , ImPlotAxisFlags_NoInitialFit, ImPlotAxisFlags_NoInitialFit);
1011+ ImPlot::SetNextLineStyle (ImVec4 (1 , 1 , 1 , 0.5 ));
1012+ ImPlot::SetNextColorsData (ImPlotCol_Line, colors);
1013+ ImPlot::PlotVLines (" VLines" , vals, 3 );
1014+ ImPlot::SetNextLineStyle (ImVec4 (1 , 1 , 1 , 0.5 ));
1015+ ImPlot::SetNextColorsData (ImPlotCol_Line, colors);
1016+ ImPlot::PlotHLines (" HLines" , vals, 3 );
1017+ ImPlot::EndPlot ();
1018+ }
1019+ }
1020+ }
1021+
9291022void ShowDemo_LogAxes () {
9301023 static double xs[1001 ], ys1[1001 ], ys2[1001 ], ys3[1001 ];
9311024 for (int i = 0 ; i < 1001 ; ++i) {
@@ -2032,6 +2125,8 @@ void ShowDemoWindow(bool* p_open) {
20322125 ShowDemo_Images ();
20332126 if (ImGui::CollapsingHeader (" Markers and Text" ))
20342127 ShowDemo_MarkersAndText ();
2128+ if (ImGui::CollapsingHeader (" Plot Colors" ))
2129+ ShowDemo_PlotColors ();
20352130 ImGui::EndTabItem ();
20362131 }
20372132 if (ImGui::BeginTabItem (" Subplots" )) {
0 commit comments