customViews

Description

Static Custom Views

This allows you to add custom views in a specific location on the dashboard.

Types

export type BaseCustomView = {
  id: string;
};
 
export type StaticCustomView = BaseCustomView & {
  /* The index of the row where the component will be placed. */
  rowIndex: number;
  /* The index of the cell where the component will be placed. */
  cellIndex: number;
  /* The component to render. 
  The component can be a React component or a string.
  */
  Component: (params: CustomViewCallbackParams) => JSX.Element | string;
  /* The options for the component. */
  options?: {
    /* Disable the ability to add a cell to a row where the component is placed. */
    disableAddCell?: boolean;
    /* Removes the Panel that contains the component. */
    raw?: boolean;
  };
  /* The attributes for the component. */
  attributes?: {
    /* The title of the component. */
    displayTitle?: string;
    /* The subject of the component. */
    displaySubject?: string;
  };
};
<Vizzly.Dashboard
  customViews={[
    {
      id: '<< A UNIQUE ID >>',
      Component: () => <div>Custom view</div>,
      rowIndex: 0,
      cellIndex: 0,
    },
  ]}
/>

Selectable Custom Views

These will appear in the Views dropdown in the Editor and can be selected. They can place them anywhere on the dashboard.

It's currently not possible for the end user to add properties to these components.

Types

export type BaseCustomView = {
  id: string;
};
 
export type SelectableCustomView = BaseCustomView & {
  title: string;
  Icon: () => JSX.Element;
  Component: (params: CustomViewCallbackParams) => JSX.Element | string;
};
<Vizzly.Dashboard
  customViews={[
    {
      id: '<< A UNIQUE ID >>',
      Component: () => <div>Custom view</div>,
      title: '<< A TITLE >>',
      Icon: () => <div style={{ width: 20, height: 20 }} />,
    },
  ]}
/>

Using the property with Vizzly Query Engine

The Component in the Custom View makes available dashboardFilters, this contains the selected filters on the dashboard. If you want your component to behave based on what the user has selected you can pass this into your query and it will only return data that matches the filters.

You can use the customViews in conjunction with the query API to build your own custom components.

Types

export type DataSetDashboardFilters = { [dataSetId: string]: QueryAttributes.Filter[][] };
 
export type CustomViewCallbackParams = {
  dashboardFilters: DataSetDashboardFilters;
  attributes?: Component.CustomViewAttributes;
};

Example

import React, { Fragment, useEffect, useState } from 'react';
import { Dashboard } from '@vizzly/dashboard';
import { QueryResponse, Vizzly } from '@vizzly/services';
 
export default () => {
  const [vizzlyLoaded, setVizzlyLoaded] = useState<boolean>(false);
 
  useEffect(() => {
    async function initializeVizzly() {
      try {
        await Vizzly.load({
          queryEngine: 'https://example.vizzly.co/query-engine',
          identity: getIdentity('new_user'),
        });
        const instance = await Vizzly.waitToUse();
        setVizzlyLoaded(!!instance);
      } catch (err) {
        console.error(err);
      }
    }
 
    initializeVizzly();
  }, []);
 
  if (!vizzlyLoaded) {
    return <div>Loading...</div>;
  }
 
  return (
    <Dashboard
      customViews={[
        {
          id: 'string',
          Component: (params: CustomViewCallbackParams) => (
            // In your code, add a component like we have in this example with CustomView.
            <CustomChart dashboardFilters={params?.dashboardFilters} />
          ),
          rowIndex: 1,
          cellIndex: 1,
        },
      ]}
    />
  );
};
 
const CustomChart: FunctionComponent<{ dashboardFilters?: DataSetDashboardFilters }> = ({ dashboardFilters }) => {
  const [componentResponse, setComponentResponse] = useState<QueryResponse | null>(null);
 
  useEffect(() => {
    const doSomething = async () => {
      const vizzly = Vizzly.use();
      const response: QueryResponse = await vizzly.query(
        [
          {
            filter: [],
            measure: [
              {
                field: 'fie_1',
                function: 'sum',
              },
            ],
            dimension: [
              {
                field: 'fie_1',
                function: 'none',
                pivot: 'x',
              },
            ],
            timeDimension: null,
            dataSetId: 'data_set_1',
            order: [],
            limit: 5000,
          },
        ],
        // here we pass in the dashboardFilters brought in from the parent component
        // and used in your custom query.
        { dashboardFilters }
      );
      setComponentResponse(response);
    };
 
    doSomething();
  }, [dashboardFilters]);
 
  if (componentResponse === null) return <div>Loading...</div>;
 
  return <div>{JSON.stringify(componentResponse.results)}</div>;
};