How to create and embedd Visualizaitions in kibana plugin

Hi,

I am trying to create a visualization from gui in kibana. and then trying to fetch that visualization (later will export that visualization as json and include it in the package) into kibana plugin using visualizations plugin in kibana 7.10.0.

here is the code.

const loadvis = async () => {
    const vises = visualizations.all()
    const all_saved_vis = await visualizations.savedVisualizationsLoader.find('testVis').then(async (response) => {
        setVisHits(response.hits[0]);
        console.log(response.hits[0]);
        const response_hits_0_visState = response.hits[0];
        response_hits_0_visState["visState"] = JSON.parse(response.hits[0]["visState"]);
        const TestVis = visualizations.convertToSerializedVis(response_hits_0_visState);
        console.log("TestVis :",TestVis)
        console.log("TestVis.data.searchSource :",TestVis.data.searchSource)
        const x:SearchSourceFields = {
          index: indexPattern
        }
        TestVis.data.searchSource = x

        const SerialVis = await visualizations.createVis('histogram', TestVis).then(async (VIS) => {
          const emb = await visualizations.__LEGACY.createVisEmbeddableFromObject(VIS, { id: "testOne" },visualizations.savedVisualizationsLoader)
            .then((embbed) => {
              const htmlele = document.getElementById("test1");
              setEmbeddable(embbed);
              embbed.render(htmlele)
              console.log("embedd :", embbed);
              return embbed;
            }
          );
          console.log("embedd :", emb);
          return VIS;
        }, (reason) => { return reason; });
        console.log("VIS :", SerialVis);
        return response;
      },(reason) => {return reason})
    console.log("Visualizations :",vises)
    console.log('all_saved_vis :',all_saved_vis)
  }

embbed.render(htmlele) does not seem to work.

this does not throw any error nor does it render the visualization.

is this the correct way I am doing? please help

Thanks and regards
K Chaitanya M Rao

Hi @K_Chaitanya

await visualizations.createVis('histogram', TestVis)

I don't think this is correct, afaik, this is used to register a new visualization type and not to create instance of visualization type.

I think you should try to use visualizeEmbeddableFactory.createFromSavedObject instead https://github.com/elastic/kibana/blob/master/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx#L141

savedObjectId is testVis and using input I think you can override searchSource.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.