2

After reading almost all I found here and in google, I've not found a simple and direct solution/example about plotting x-y values in c++ (console app win32, the black one) in visual studio 2008. Based on this post, I should use MsChart controls, would anyone be willing please to share a simple example about this?

This post, and many others, talk about libraries that don't belong to visual studio (so they won't compile with the EXE, hence it won't be included as part of the resulting EXE) or having the graphic displayed with excel. I just want to get a simple x-y graphic (and line going through these points), not worried at all if it is the ugliest and simplest graphic in the whole world, but it must appear automatically after my code run (perhaps in a new window or perhaps inside the console?) AND if I run this EXE in another pc, thousands of kilometers away from my pc, the graphic will still appear after the code runs.

Am I asking something too complex? boring?? I hope I could get some answers/examples/solutions instead of this post being closed =) Thanks in advance!


@Koushik thanks again for your support! even though I'm getting the whole picture I need a clear example to get it right, so to voteup I'd like to test this simple example:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double f(const double &x) {
    return (
        sin(x)*exp(-x));
}

int main()
{
    unsigned Nplot = 100;
    double x_low = 0.0;
    double x_high = 20.0;
    double h = (x_high - x_low)/(double)Nplot;

    for (unsigned i=0;i<=Nplot;i++)
    {
        double x = x_low + i*h;
        cout << setw(20) << x <<
                setw(20) << f(x) << endl;
    }

    system("pause>nul");
    return 0;
}

this is a pretty basic code to generate a sin(x) function. If you copy/paste this in console application Win32 in VC++ 2008 (new project/Visual C++/Win32/console application Win32/empty project/), the data will appear in the screen:

enter image description here

Then, putting these values in Excel gives:

enter image description here

I want this, BUT the output window should appear after the values appear in the console, and more important, the code must not depend on excel or something else external to the visual C++. So, writing some additional code to draw this line and after compiling, both must appear/work in any computer, especially in those without VS or any libraries, just running the EXE in windows, that's all. So, is it possible? could you share a simple code example to draw this line (to add it to the above code)? thanks!

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Gery
  • 8,390
  • 3
  • 22
  • 39
  • may be you can check this out.these are not in VB but might help.[SO](http://stackoverflow.com/questions/1937163/drawing-in-a-win32-console-on-c). Alternatively you could call a gui application through the console once you have all the data for the graph ready. – Koushik Shetty Feb 26 '13 at 12:26
  • correct your question. it visual studio c++ – Koushik Shetty Feb 26 '13 at 12:32
  • corrections are made, thanks for pointing that out!. I'll do a windows form after this test, but first want to do it in console win32 because I find it's easier for testing. – Gery Feb 26 '13 at 12:36
  • btw, how could I call a gui application through the console after the data are ready? but this gui application will be included in the EXE? – Gery Feb 26 '13 at 12:43

2 Answers2

0

you can call gui application or rather any other app by using the system("appname");. include the stdlib.h header. what you can do is create your console app and prepare the data. store those data in a file, open the app which can read the data from the file and plot it.

The gui application will not be so difficult to write either, if your goal is to only plot the data.in console you really cant draw things. in turbo c++ we had this option because it was mainly created for dos.

Algorithm: 1) prepare the data say time along x axis and amplitude along y axis. store this in a file using your own format.eg: x-ais data y-axis data .

2).Prepare a gui app that can plot the graph. use CreateGraphics() methods of the form to draw lines i.e the axes and the data line. CreateGraphics::drawLine() and CreateGraphics::drawLines() should help your progress.

EDIT: you can pass command line args to the GUI app. this argument could be the data file path

Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
  • thanks! about point 2, so in the end the graph will appear after I run the EXE program right? I mean, if I run the EXE in different computers, it wont be any problems? – Gery Feb 26 '13 at 20:12
  • yes after you collect the data and store it in a file and then call the GUI app. `Running on different computers`, yes you can as long as both the console and GUI app is present as it is present in your system. lastly if this has helped mark it as answer.:-)comment for more clarification – Koushik Shetty Feb 27 '13 at 04:43
0

The simplest thing would be for your console application, after computing the data, to create a window on screen (yes, console applications are allowed to use GUI) and then handle WM_PAINT with BeginPaint/PolyLine/EndPaint, using some appropriate scaling that makes the data (1) rightside up and (2) fit to the window.

You won't have any grid lines or axis labels, just the plotted line, but that's the bare minimum and can easily be done without either using or recreating an entire graphing library.

For an example of how to add GUI to your program, try Raymond Chen's scratch program, C++ version. Quite a bit of code to create just one window, but lucky for you, the only thing you have to change is the PaintContent() function.

Using Polyline is almost as straightforward as you could ask for -- it accepts an array of datapoints.

If you really want to add gridlines and labels, more Polyline calls and the ExtTextOut function will do the trick, but soon after that you'll want CreatePen and SelectObject to give control over line colors and styles (dotted, dashed, etc). And pay attention to the order -- draw the bottom things first so that later calls can cover over the top.


On the other hand if a single executable is not a requirement, I suggest you ship a copy of Gnuplot (the pgnuplot executable is designed for invocation from another program). First use wgnuplot to tune your plot options, then pass them to pgnuplot along with your data. You'll get reasonable scaling, gridlines, and labels for free this way, as well as exporting the result into common formats such as PNG.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720