* This class allows dynamic plots, ie changing the plotted data without clearing and re-plotting */
| 2398 | * This class allows dynamic plots, ie changing the plotted data without clearing and re-plotting |
| 2399 | */ |
| 2400 | class Plot |
| 2401 | { |
| 2402 | public: |
| 2403 | // default initialization with plot label, some data and format |
| 2404 | template<typename Numeric> |
| 2405 | Plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "") { |
| 2406 | detail::_interpreter::get(); |
| 2407 | |
| 2408 | assert(x.size() == y.size()); |
| 2409 | |
| 2410 | PyObject* kwargs = PyDict_New(); |
| 2411 | if(name != "") |
| 2412 | PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str())); |
| 2413 | |
| 2414 | PyObject* xarray = detail::get_array(x); |
| 2415 | PyObject* yarray = detail::get_array(y); |
| 2416 | |
| 2417 | PyObject* pystring = PyString_FromString(format.c_str()); |
| 2418 | |
| 2419 | PyObject* plot_args = PyTuple_New(3); |
| 2420 | PyTuple_SetItem(plot_args, 0, xarray); |
| 2421 | PyTuple_SetItem(plot_args, 1, yarray); |
| 2422 | PyTuple_SetItem(plot_args, 2, pystring); |
| 2423 | |
| 2424 | PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs); |
| 2425 | |
| 2426 | Py_DECREF(kwargs); |
| 2427 | Py_DECREF(plot_args); |
| 2428 | |
| 2429 | if(res) |
| 2430 | { |
| 2431 | line= PyList_GetItem(res, 0); |
| 2432 | |
| 2433 | if(line) |
| 2434 | set_data_fct = PyObject_GetAttrString(line,"set_data"); |
| 2435 | else |
| 2436 | Py_DECREF(line); |
| 2437 | Py_DECREF(res); |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | // shorter initialization with name or format only |
| 2442 | // basically calls line, = plot([], []) |
| 2443 | Plot(const std::string& name = "", const std::string& format = "") |
| 2444 | : Plot(name, std::vector<double>(), std::vector<double>(), format) {} |
| 2445 | |
| 2446 | template<typename Numeric> |
| 2447 | bool update(const std::vector<Numeric>& x, const std::vector<Numeric>& y) { |
| 2448 | assert(x.size() == y.size()); |
| 2449 | if(set_data_fct) |
| 2450 | { |
| 2451 | PyObject* xarray = detail::get_array(x); |
| 2452 | PyObject* yarray = detail::get_array(y); |
| 2453 | |
| 2454 | PyObject* plot_args = PyTuple_New(2); |
| 2455 | PyTuple_SetItem(plot_args, 0, xarray); |
| 2456 | PyTuple_SetItem(plot_args, 1, yarray); |
| 2457 |
nothing calls this directly
no outgoing calls
no test coverage detected