| 818 | //----------------------------------------------------------------------------- |
| 819 | |
| 820 | void Demo_Histogram() { |
| 821 | IMGUI_DEMO_MARKER("Plots/Histogram"); |
| 822 | static ImPlotHistogramFlags hist_flags = ImPlotHistogramFlags_Density; |
| 823 | static int bins = 50; |
| 824 | static double mu = 5; |
| 825 | static double sigma = 2; |
| 826 | ImGui::SetNextItemWidth(200); |
| 827 | if (ImGui::RadioButton("Sqrt",bins==ImPlotBin_Sqrt)) { bins = ImPlotBin_Sqrt; } ImGui::SameLine(); |
| 828 | if (ImGui::RadioButton("Sturges",bins==ImPlotBin_Sturges)) { bins = ImPlotBin_Sturges; } ImGui::SameLine(); |
| 829 | if (ImGui::RadioButton("Rice",bins==ImPlotBin_Rice)) { bins = ImPlotBin_Rice; } ImGui::SameLine(); |
| 830 | if (ImGui::RadioButton("Scott",bins==ImPlotBin_Scott)) { bins = ImPlotBin_Scott; } ImGui::SameLine(); |
| 831 | if (ImGui::RadioButton("N Bins",bins>=0)) { bins = 50; } |
| 832 | if (bins>=0) { |
| 833 | ImGui::SameLine(); |
| 834 | ImGui::SetNextItemWidth(200); |
| 835 | ImGui::SliderInt("##Bins", &bins, 1, 100); |
| 836 | } |
| 837 | ImGui::CheckboxFlags("Horizontal", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Horizontal); |
| 838 | ImGui::SameLine(); |
| 839 | ImGui::CheckboxFlags("Density", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Density); |
| 840 | ImGui::SameLine(); |
| 841 | ImGui::CheckboxFlags("Cumulative", (unsigned int*)&hist_flags, ImPlotHistogramFlags_Cumulative); |
| 842 | |
| 843 | static bool range = false; |
| 844 | ImGui::Checkbox("Range", &range); |
| 845 | static float rmin = -3; |
| 846 | static float rmax = 13; |
| 847 | if (range) { |
| 848 | ImGui::SameLine(); |
| 849 | ImGui::SetNextItemWidth(200); |
| 850 | ImGui::DragFloat2("##Range",&rmin,0.1f,-3,13); |
| 851 | ImGui::SameLine(); |
| 852 | ImGui::CheckboxFlags("Exclude Outliers", (unsigned int*)&hist_flags, ImPlotHistogramFlags_NoOutliers); |
| 853 | } |
| 854 | static NormalDistribution<10000> dist(mu, sigma); |
| 855 | static double x[100]; |
| 856 | static double y[100]; |
| 857 | if (hist_flags & ImPlotHistogramFlags_Density) { |
| 858 | for (int i = 0; i < 100; ++i) { |
| 859 | x[i] = -3 + 16 * (double)i/99.0; |
| 860 | y[i] = exp( - (x[i]-mu)*(x[i]-mu) / (2*sigma*sigma)) / (sigma * sqrt(2*3.141592653589793238)); |
| 861 | } |
| 862 | if (hist_flags & ImPlotHistogramFlags_Cumulative) { |
| 863 | for (int i = 1; i < 100; ++i) |
| 864 | y[i] += y[i-1]; |
| 865 | for (int i = 0; i < 100; ++i) |
| 866 | y[i] /= y[99]; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (ImPlot::BeginPlot("##Histograms")) { |
| 871 | ImPlot::SetupAxes(nullptr,nullptr,ImPlotAxisFlags_AutoFit,ImPlotAxisFlags_AutoFit); |
| 872 | ImPlot::PlotHistogram("Empirical", dist.Data, 10000, bins, 1.0, range ? ImPlotRange(rmin,rmax) : ImPlotRange(), { |
| 873 | ImPlotProp_FillAlpha, 0.5f, |
| 874 | ImPlotProp_Flags, hist_flags |
| 875 | }); |
| 876 | if ((hist_flags & ImPlotHistogramFlags_Density) && !(hist_flags & ImPlotHistogramFlags_NoOutliers)) { |
| 877 | if (hist_flags & ImPlotHistogramFlags_Horizontal) |
nothing calls this directly
no test coverage detected