Line graph


#1

Hey all,
Another newbie question. What is the most simple way to put a plot into a component? Just a simple line graph. The simpler the better. It doesn’t need to update very often. All suggestions welcomed. Thanks in advance.


#2

Just draw the lines… Use a Path, add your lines to it, then draw a stroke along it.


#3

Thanks for the nudge in the right direction. I was looking at this for some inspiration . . .

http://code.google.com/p/dspfilterscpp/source/browse/trunk/DspJuceDemo.cpp

. . . but using that source I was getting build errors that I was unable to fix with my current level of C++ competence. Anyhow, I found the Doxygen Path page and should be able to cobble something together from there. Many thanks.


#4

Hi again. This is actually going surprisingly well ( shock :!: ) but I am having some trouble putting the icing on the cake. I would like to make a dashed line (I’m guessing using PathStrokeType::createDashedStroke) but I can’t get it to work for the life of me. Could anyone enlighten me with the magic line of code required to make a dashed line from the following code snippet.

Path linReference; linReference.startNewSubPath (0, getHeight()); linReference.lineTo (getWidth(), 0); g.setColour (Colours::cornflowerblue.withAlpha (0.5f)); g.strokePath (linReference, PathStrokeType (3.0f));


#5

You’d need to call PathStrokeType::createDashedStroke to generate a new path from your original one, then you’d just fill the new path.


#6

Easily said :slight_smile: . Well, after hacking away at this and being greeted with a whole new bunch of compile errors that I’ve not seen before (it has been a good learning expirience), I have managed to get something working.

Path linReference;
linReference.startNewSubPath (0, getHeight());
linReference.lineTo (getWidth(), 0);

PathStrokeType *dasher;	   
dasher = new PathStrokeType(3.0);
float Dashes[] = { 20, 10 };
dasher->createDashedStroke(linReference, linReference, Dashes, 2);

g.setColour (Colours::cornflowerblue.withAlpha (0.5f));
g.strokePath (linReference, PathStrokeType (3.0f));

Is that the right kind of idea or have I just made code spaghetti? Ta much. PS Juce is awesome!


#7

You’re leaking like a sieve there. Never use ‘new’ if you can use a stack object instead.

More like this:

[code]Path linReference;
linReference.startNewSubPath (0, getHeight());
linReference.lineTo (getWidth(), 0);

float Dashes[] = { 20, 10 };
PathStrokeType (3.0f).createDashedStroke (linReference, linReference, Dashes, 2);

g.setColour (Colours::cornflowerblue.withAlpha (0.5f));
g.fillPath (linReference);[/code]


#8

AHA! That looks more elegant :slight_smile: Sorry to make your eyes bleed with my code. Anyhow, the plot is up, running, and most importantly functioning as expected. Joy.

Thanks again.


#9

I’ve seen far worse atrocities than that on the pages of this forum!