Unexpected Bar chart behavior

Hello folks,
I am seeking some help editing the contents of a bar chart at runtime in LVGL 8.3.11.

Here is the plotting function:

void drawAutoScaledBarGraph(uint8_t numOfBars, int values[], String xTicks[]) {

  lv_chart_series_t* ui_mainGraph_series_1 = lv_chart_add_series(ui_mainGraph, lv_color_hex(0x686868), LV_CHART_AXIS_PRIMARY_Y);

  // Count the number of non-zero values
  int nonZeroCount = 0;
  for (int i = 0; i < numOfBars; i++) {
    if (values[i] > 0) {
      nonZeroCount++;
    }
  }

  // Dynamically allocate memory for the series array
  lv_coord_t* ui_mainGraph_series_1_array = new lv_coord_t[nonZeroCount];

  // Loop through each bar and draw it only if the value is greater than 0
  int drawnBars = 0;
  for (int i = 0; i < numOfBars; i++) {
    if (values[i] > 0 && values[i] < 60) {
      ui_mainGraph_series_1_array[drawnBars] = values[i];
      drawnBars++;
    }
  }

  lv_chart_set_axis_tick(ui_mainGraph, LV_CHART_AXIS_PRIMARY_X, 10, 0, drawnBars, 1, true, 50);
  lv_chart_set_ext_y_array(ui_mainGraph, ui_mainGraph_series_1, ui_mainGraph_series_1_array);

  // Print the number of drawn bars
  Serial.print("Drawn Bars:\t");
  Serial.println(drawnBars);
}

The expectation is to see a bar chart with number of bars matching the count in drawnBars.

However, this is the result. Looks like multiple bars are drawn in clusters at each “xtick”

You missunderstand lvgl and mix C and C++ , but lvgl is C library…
Primary your array must exist in time = not be local or deleted.

Thank you for your response. While I understand that LVGL is a C library, your reply didn’t provide the specific guidance I need to solve my issue. Could you please offer a more detailed explanation or example on how to solve this?
Thank you!

maybe add static here helps you , but primary you dont show how and where you call this func and how your lv handler is called, where is doing real rendering. Too good is use

lv_chart_refresh

This was solved by clearing the appropriate flags:

  lv_obj_clear_flag(chart, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_PRESS_LOCK | LV_OBJ_FLAG_CLICK_FOCUSABLE | LV_OBJ_FLAG_GESTURE_BUBBLE | LV_OBJ_FLAG_SNAPPABLE);