code: def _generate_chart(
      chart_type: ChartType,
      data: Data | None,
      x_from_user: str | None = None,
      y_from_user: str | Sequence[str] | None = None,
      color_from_user: str | Color | List[Color] | None = None,
      size_from_user: str | float | None = None,
      width: int = 0,
      height: int = 0,
  ) -> Tuple[alt.Chart, AddRowsMetadata]:
      """Function to use the chart's type, data columns and indices to figure out the chart's spec."""
      import altair as alt
  
      df = type_util.convert_anything_to_df(data, ensure_copy=True)
  
      # From now on, use "df" instead of "data". Deleting "data" to guarantee we follow this.
      del data
  
      # Convert arguments received from the user to things Vega-Lite understands.
      # Get name of column to use for x.
      x_column = _parse_x_column(df, x_from_user)
      # Get name of columns to use for y.
      y_column_list = _parse_y_columns(df, y_from_user, x_column)
      # Get name of column to use for color, or constant value to use. Any/both could be None.
      color_column, color_value = _parse_generic_column(
          df, color_from_user
      )
      # Get name of column to use for size, or constant value to use. Any/both could be None.
      size_column, size_value = _parse_generic_column(
          df, size_from_user
      )
  
      # Store some info so we can use it in add_rows.
      add_rows_metadata = AddRowsMetadata(
          # The last index of df so we can adjust the input df in add_rows:
          last_index=last_index_for_melted_dataframes(df),
          # This is the input to prep_data (except for the df):
          columns=dict(
              x_column=x_column,
              y_column_list=y_column_list,
              color_column=color_column,
              size_column=size_column,
          ),
      )
  
      # At this point, all foo_column variables are either None/empty or contain actual
      # columns that are guaranteed to exist.
  
      df, x_column, y_column, color_column, size_column = prep_data(
          df, x_column, y_column_list, color_column, size_column
      )
  
      # At this point, x_column is only None if user did not provide one AND df is empty.
  
      # Create a Chart with x and y encodings.
      chart = alt.Chart(
          data=df,
          mark=chart_type.value["mark_type"],
          width=width,
          height=height,
      ).encode(
          x=_get_x_encoding(df, x_column, x_from_user, chart_type),
          y=_get_y_encoding(df, y_column, y_from_user),
      )
  
      # Set up opacity encoding.
      opacity_enc = _get_opacity_encoding(chart_type, color_column)
      if opacity_enc is not None:
          chart = chart.encode(opacity=opacity_enc)
  
      # Set up color encoding.
      color_enc = _get_color_encoding(
          df, color_value, color_column, y_column_list, color_from_user
      )
      if color_enc is not None:
          chart = chart.encode(color=color_enc)
  
      # Set up size encoding.
      size_enc = _get_size_encoding(chart_type, size_column, size_value)
      if size_enc is not None:
          chart = chart.encode(size=size_enc)
  
      # Set up tooltip encoding.
      if x_column is not None and y_column is not None:
          chart = chart.encode(
              tooltip=_get_tooltip_encoding(
                  x_column,
                  y_column,
                  size_column,
                  color_column,
                  color_enc,
              )
          )
  
      return chart.interactive(), add_rows_metadata
  
imports: from __future__ import annotations
  from contextlib import nullcontext
  from datetime import date
  from enum import Enum
  from typing import TYPE_CHECKING, Any, Collection, Dict, List, Sequence, Tuple, cast
  import pandas
  from pandas.api.types import infer_dtype, is_integer_dtype
  from typing_extensions import Literal
  import streamlit.elements.arrow_vega_lite
  from streamlit import type_util
  from streamlit.color_util import Color, is_color_like, is_color_tuple_like, is_hex_color_like, to_css_color
  from streamlit.elements.altair_utils import AddRowsMetadata
  from streamlit.elements.arrow import Data
  from streamlit.elements.utils import last_index_for_melted_dataframes
  from streamlit.errors import Error, StreamlitAPIException
  from streamlit.proto.ArrowVegaLiteChart_pb2 import ArrowVegaLiteChart
  from streamlit.runtime.metrics_util import gather_metrics
label_desc: Use image node_id 4 for calling a global function with example usage: _generate_chart(chart_type, data, x_from_user, y_from_user, color_from_user, size_from_user, width, height) and returns: chart,  add_rows_metadata