Wpiumi pabasara
Wpiumi pabasara

Reputation: 79

How to draw a plot chart using visual studio c++

I want to create a plot chart using visual studio with c++ code. The chart should be based on two axis. "x" axis display the time and "y" axis display the array data. array data have 100 elements and one data read in one second. How do I implement the code using any other graph library?

Upvotes: 7

Views: 28181

Answers (4)

Tom Huntington
Tom Huntington

Reputation: 3415

I just decided to use gnuplot lol (although you need wslg). All credit for the following goes to the tutorial here http://www.gnuplotting.org/plotting-data/

Write you data to a text file from your msvc program.

# data.dat
# X   Y
  1   2
  2   3
  3   2
  4   1

Copy paste this gpuplot code:

# plot.plt
# Set linestyle 1 to blue (#0060ad)
set style line 1 \
    linecolor rgb '#0060ad' \
    linetype 1 linewidth 2 \
    pointtype 7 pointsize 1.5

plot 'data.dat' with linespoints linestyle 1

In wsl run:

gpuplot -p plot.plt

Upvotes: 0

The_Learner
The_Learner

Reputation: 619

Plotting is a little bit tricky job in C++, as there is no default plotting library available in any C++ IDE. However, there are many libraries available online for making plotting possible in C++. Some plotting tools like Gnuplot, PPlot, Matlab, Python, KoolPlot (May be enough for your requirement).

I have answered a similar question a few days (plotting package for c++). The answer may be helpful.

Upvotes: 0

Stepan Yakovenko
Stepan Yakovenko

Reputation: 9206

1) checkout and install Microsoft vcpkg to new folder (see 1-step instruction here: https://github.com/Microsoft/vcpkg)

2) vcpkg.exe install plplot from vcpkg folder

3) vcpkg.exe integrate project will give you instruction to add plplot to your MSVC project

4) paste this instruction to the Nuget Console:

enter image description here

5) after you paste and project reloads you can try this code:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include "plplot\plstream.h"
using namespace std;
const int NSIZE = 101;
int main(int argc, char ** argv) {
    PLFLT x[NSIZE], y[NSIZE];
    PLFLT xmin = 0., xmax = 1., ymin = 0., ymax = 100.;
    int   i;
    for (i = 0; i < NSIZE; i++) {
        x[i] = (PLFLT)(i) / (PLFLT)(NSIZE - 1);
        y[i] = ymax * x[i] * x[i];
    }
    auto pls = new plstream();
    plsdev("wingcc");
    pls->init();
    pls->env(xmin, xmax, ymin, ymax, 0, 0);
    pls->lab("x", "y=100 x#u2#d", "Simple PLplot demo of a 2D line plot");
    pls->line(NSIZE, x, y);
    delete pls;
}

and you get:

enter image description here

tested on MSVC2015

Upvotes: 6

Wagner Patriota
Wagner Patriota

Reputation: 5674

I answered a very similar question a few years ago... there's a simple, straight and compilable example: Graphical representation - Data Distribution

Obviously, the chart is not the same one that you need. But you can modify it in order to draw anything you want using C++ and then make any chart.

Upvotes: 0

Related Questions