* @brief Append a new series of any drawing style to the plot. * * Allocates internal buffers and deep-copies @p xData (if non-NULL), * @p yData, and @p label into them. The series is stored in the next * free slot of the plot's fixed-capacity series table and given the * supplied colour and drawing style. * * If @p xData is NULL the renderer falls back to using the implicit * indices 0..d
| 426 | * @endcode |
| 427 | */ |
| 428 | int plot_add_series(Plot* plot, const float* xData, const float* yData, size_t dataSize, const char* label, PlotType pltype, PlotColor color) { |
| 429 | PLOT_LOG("[plot_add_series]: enter (plot=%p, dataSize=%zu, label=%s, pltype=%d, seriesCount=%zu)", (void*)plot, dataSize, label ? label : "(null)", (int)pltype, |
| 430 | plot ? plot->seriesCount : (size_t)0); |
| 431 | |
| 432 | if (!plot || !yData || dataSize == 0 || plot->seriesCount >= PLOT_MAX_SERIES) { |
| 433 | PLOT_LOG("[plot_add_series]: Invalid args or max series reached -> -1"); |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | float* xCopy = NULL; |
| 438 | if (xData) { |
| 439 | xCopy = (float*)malloc(sizeof(float) * dataSize); |
| 440 | if (!xCopy) { |
| 441 | PLOT_LOG("[plot_add_series]: OOM xCopy -> -1"); |
| 442 | return -1; |
| 443 | } |
| 444 | memcpy(xCopy, xData, sizeof(float) * dataSize); |
| 445 | } |
| 446 | |
| 447 | float* yCopy = (float*)malloc(sizeof(float) * dataSize); |
| 448 | if (!yCopy) { |
| 449 | PLOT_LOG("[plot_add_series]: OOM yCopy -> -1"); |
| 450 | free(xCopy); |
| 451 | |
| 452 | return -1; |
| 453 | } |
| 454 | memcpy(yCopy, yData, sizeof(float) * dataSize); |
| 455 | |
| 456 | char* labelCopy = NULL; |
| 457 | if (label) { |
| 458 | labelCopy = p_strdup(label); |
| 459 | if (!labelCopy) { |
| 460 | PLOT_LOG("[plot_add_series]: OOM labelCopy -> -1"); |
| 461 | free(xCopy); |
| 462 | free(yCopy); |
| 463 | |
| 464 | return -1; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | size_t idx = plot->seriesCount++; |
| 469 | plot->series[idx].xData = xCopy; |
| 470 | plot->series[idx].yData = yCopy; |
| 471 | plot->series[idx].dataSize = dataSize; |
| 472 | plot->series[idx].label = labelCopy; |
| 473 | plot->series[idx].color = color; |
| 474 | plot->series[idx].pltype = pltype; |
| 475 | |
| 476 | PLOT_LOG("[plot_add_series]: exit -> index=%d (%s)", (int)idx, label ? label : "(no label)"); |
| 477 | return (int)idx; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /** |
no test coverage detected