How to use c++ to generate a picture using Graphviz?

I am a newbee for graphviz, what I want to do is generate a picture that accept a json which describes a graph, I want to call the api in my application, and I want to use cpp as my language.

I read the document but but just found some easy and simple case

## Sample Programs using Graphviz

* [demo.c](
* [dot.c](
* [example.c](
* [simple.c](
* [Makefile](

I know how to do it using the command line tool, but I am confused about how do do it in the program.

Is there any cpp lib?

Oh, sorry, just see the link,, maybe it’s what i want

You should be able to include and call the C api from C++, as C++ is (almost) a superset of C.

However for a network API, I’d suggest/consider running the command line program as a sandboxed subprocess; graphviz has a few bugs that cause it to crash, and graphviz isn’t thread-safe (important if you want to handle multiple parallel requests). Subprocesses work around these issues. Just my 2c

thanks for your suggestion, though I am still not as far as you said. :grin:

The guide you linked is indeed the place to start. Unfortunately it is quite out of date. Any contributions to improve it would be much appreciated.

So is there any nice examples?

Are you looking for more examples other than the examples you’ve already found? If so, we don’t have any ready made. You’ll have to search outside of the Graphviz project. If you need help with those examples or something completely different; please specify what you need help with.

Oh, sorry, let me clarify what I want.

Here is my case, I have got a graph json data from upstream, I want to draw a picture and send it to the webpage.

The graph json data would be like [[a,b], [a, c], [b,c]] , which means a goes to b, a goes to c, b goes to c, I thought maybe I should (1) transfer the json data into dot language used by GraphViz, and (2)use the command line to generate a picture binary ,and then I should (3) transfer the picture binary into a base64-encoded string and give it to the webpage.

That is what I have thought at the first step, I dont know if there is any better practice, since I don’t know the dot language well, and I am confused by these cases or demos in the PDF.

SO, I have a little trouble now.

Maybe I should use the clib Graphviz interface to generate the base64-encoded picture string to privide to the frontend, so this may be what I prefered, but I dont know if the clib interface is easy to learn, or if the clib interface just take the dot language as parameter.

Is it easy to load the command line to generate picture in a base64-encoded string? This seems safe but aslo complicated as you suggest.

Shameless plug: Use d3-graphviz. Write a JavaScript application that converts your JSON to DOT and uses d3-graphviz to render an SVG and directly. Then send your JSON to it. Or, if you don’t want to write as much JavaScript, use your favorite language to convert JSON to DOT locally and and write a d3-graphviz based JavaScript application that just renders it.

It’s a pity that I could not control the frontend, because the way we cooperate is I should offer the picture content as a backend developer…

I have discussed with my colleage to try to find a better solution, maybe your advice is good suitable for me.

Seems that you’re in need of an organizational consultant more than technical help :wink:. I would use Python to accomplish what you want. Parsing JSON comes out of the box and there are plenty of Graphviz Python bindings to chose from.

I would also use python to do what you want.