|
| 1 | +--- |
| 2 | +title: Render GoodData Visualizations in Next.js |
| 3 | +linkTitle: Next.js |
| 4 | +copyright: (C) 2007-2018 GoodData Corporation |
| 5 | +--- |
| 6 | + |
| 7 | +Next.js is not supported out-of-the-box by GoodData.UI. |
| 8 | + |
| 9 | +However, rendering the GoodData visualizations in your Next.js project is not as daunting as it seems. This article serves as a quick demo of how to integrate GoodData visualizations into your project. |
| 10 | +### Step 1: Install all necessary dependencies |
| 11 | + |
| 12 | +The following packages deal with GoodData backend: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm i @gooddata/api-client-tiger @gooddata/sdk-backend-tiger |
| 16 | +``` |
| 17 | + |
| 18 | +The following package contains *all* components of GoodData UI SDK: |
| 19 | + |
| 20 | +```bash |
| 21 | +npm i @gooddata/sdk-ui-all |
| 22 | +``` |
| 23 | + |
| 24 | +{{% alert title="Install only some components"%}} |
| 25 | +If you do not need to install all components, you can select only those you need. For more information refer to the [documentation](../../../learn/integrate_and_authenticate/cn_and_cloud_integration/#IntegrateCNandCloudintoanExistingApplication-Step1Installthenecessarydependencies). |
| 26 | +{{% /alert %}} |
| 27 | + |
| 28 | +The following package will help you generate a file with code representation of all available metrics and attributes in your GoodData Cloud: |
| 29 | + |
| 30 | +```bash |
| 31 | +npm i -D @gooddata/catalog-export |
| 32 | +``` |
| 33 | + |
| 34 | +### Step 2: Extend package.json |
| 35 | + |
| 36 | +You need to modify `package.json`, and add the following properties: |
| 37 | + |
| 38 | +```bash |
| 39 | +"gooddata": { |
| 40 | + "hostname": "<your-gooddata-host>", |
| 41 | + "workspaceId": "<your-workspace-id>", |
| 42 | + "catalogOutput": "app/gooddata-export.ts", |
| 43 | + "backend": "tiger" |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +This configuration is for the `catalog-export` tool, which exports all available metrics and attributes to a single file that you can reference in your code. |
| 48 | + |
| 49 | +### Step 3: Add a new script to package.json |
| 50 | + |
| 51 | +```bash |
| 52 | +"scripts": { |
| 53 | + "dev": "next dev", |
| 54 | + "build": "next build", |
| 55 | + "start": "next start", |
| 56 | + "lint": "next lint", |
| 57 | + "refresh-md": "gdc-catalog-export" <-- this is the new script you need to add! |
| 58 | +}, |
| 59 | +``` |
| 60 | + |
| 61 | +### Step 4: Add API token |
| 62 | + |
| 63 | +```bash |
| 64 | +export TIGER_API_TOKEN=<your-api-token> |
| 65 | +``` |
| 66 | + |
| 67 | +*Note: more info on how to get API token is in the [documentation](https://www.gooddata.com/developers/cloud-native/doc/cloud/getting-started/create-api-token/).* |
| 68 | + |
| 69 | +### Step 5: Configure CORS in GoodData |
| 70 | + |
| 71 | +If your Next.js application runs on a different domain than GoodData (which is the most probable scenario), you need to configure CORS. Basically, you need to go to `<your-gooddata-host>/settings` and add Cross-origin resource sharing (CORS). If your Next.js application runs on domain *https://super-cool-app.com*, you need to add *https://super-cool-app.com* to CORS. You can find more information in the [documentation](https://www.gooddata.com/developers/cloud-native/doc/cloud/manage-organization/set-up-cors-for-organization/). |
| 72 | + |
| 73 | +### Step 6: Run catalog-export tool to export metrics and attributes |
| 74 | + |
| 75 | +```bash |
| 76 | +npm run refresh-md |
| 77 | +``` |
| 78 | + |
| 79 | +The successful result is: |
| 80 | +The result generated from the workspace with id is located at `/app/gooddata-export.ts`. |
| 81 | + |
| 82 | +### Step 7: Add styles |
| 83 | + |
| 84 | +Add GoodData styles to `layout.tsx`: |
| 85 | + |
| 86 | +```javascript |
| 87 | +import "@gooddata/sdk-ui-filters/styles/css/main.css"; |
| 88 | +import "@gooddata/sdk-ui-charts/styles/css/main.css"; |
| 89 | +import "@gooddata/sdk-ui-geo/styles/css/main.css"; |
| 90 | +import "@gooddata/sdk-ui-pivot/styles/css/main.css"; |
| 91 | +import "@gooddata/sdk-ui-kit/styles/css/main.css"; |
| 92 | +import "@gooddata/sdk-ui-ext/styles/css/main.css"; |
| 93 | +``` |
| 94 | + |
| 95 | +### Step 8: Build your visualization |
| 96 | + |
| 97 | +The following code snippet contains example of simple GoodData visualization. |
| 98 | + |
| 99 | +For more information, refer to this demo [Github repo](https://github.com/patrikbraborec/gooddata-nextjs). |
| 100 | + |
| 101 | +```javascript |
| 102 | +const measures = [ReturnUnitCost.Sum]; |
| 103 | +const attributes = [DateDatasets.CustomerCreatedDate.CustomerCreatedDateQuarterOfYear.Default]; |
| 104 | + |
| 105 | +return ( |
| 106 | +<BackendProvider backend={backend}> |
| 107 | + <WorkspaceProvider workspace="<your-workspace-id>"> |
| 108 | + <div className="w-full h-full flex flex-col items-center"> |
| 109 | + <h1 className="my-10 text-2xl underline">GoodData with Next.js</h1> |
| 110 | + |
| 111 | + <div className="w-1/2 h-1/2"> |
| 112 | + <BarChart |
| 113 | + measures={measures} |
| 114 | + viewBy={attributes} |
| 115 | + /> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </WorkspaceProvider> |
| 119 | +</BackendProvider> |
| 120 | +``` |
| 121 | +
|
| 122 | +Now you can run your Next.js application with GoodData visualization. |
| 123 | +
|
| 124 | +## Run the Application |
| 125 | +
|
| 126 | +If you want to run this demo, please make sure you have configured everything correctly (with respect to GoodData) as described in the chapter above |
| 127 | +
|
| 128 | +Install all dependencies (you can skip this step if you have already installed your dependencies) |
| 129 | +
|
| 130 | +```bash |
| 131 | +npm install |
| 132 | +``` |
| 133 | +
|
| 134 | +Run development server: |
| 135 | +
|
| 136 | +```bash |
| 137 | +npm run dev |
| 138 | +``` |
| 139 | +
|
| 140 | +## Result |
| 141 | +
|
| 142 | +The following screenshot shows how this demo looks like: |
| 143 | +
|
| 144 | + |
0 commit comments