Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please share basic installation process to load d3.js graph integrated with ASP.Net Core, Angular6 application. My code is giving this error.
"Error: Unexpected token <"

What I have tried:

I am installing d3.js in my project using "npm install ng2-ndv3 --save".
Then importing in NgModule and also imported d3 and nvd3 as below.

JavaScript
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import 'd3';
import 'nvd3'
import {NvD3Module} from "ng2-nvd3";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NvD3Module,

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Then adding following code in component.ts
JavaScript
import {Component} from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  options: any;
  data: any;


  constructor() {
    this.options = {
      chart: {
        type: 'pieChart',
        height: 500,
        x: function (d) {
          return d.key;
        },
        y: function (d) {
          return d.y;
        },
        showLabels: true,
        duration: 500,
        labelThreshold: 0.01,
        labelSunbeamLayout: true,
        legend: {
          margin: {
            top: 5,
            right: 35,
            bottom: 5,
            left: 0
          }
        }
      }
    };

    this.data = [
      {
        key: "P60-1",
        y: 256
      },
      {
        key: "P60-2",
        y: 445
      },
      {
        key: "P40",
        y: 225
      },
      {
        key: "P73",
        y: 127
      },
      {
        key: "P71",
        y: 128
      }
    ];
  }
}

Then finally adding in component.html page
HTML
<pre><div>
  <nvd3 [options]="options" [data]="data"></nvd3>
</div>
Posted
Updated 21-Aug-18 7:24am

1 solution

The above error comes when one of your config json is not valid. Check your packages.json and related ones if you have valid JSON configured
 
Share this answer
 
Comments
Aurobinda Rana 27-Aug-18 8:59am    
Hi,
My package.json is as following.

{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"dependencies": {
"@angular/common": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/router": "^6.0.2",
"@angular/upgrade": "^6.0.2",
"bootstrap": "^4.1.1",
"core-js": "^2.5.6",
"ng2-nvd3": "^2.0.0",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.1.0",
"systemjs": "^0.21.3",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/cli": "^6.1.2",
"@types/core-js": "^0.9.46",
"@types/node": "^10.5.7",
"concurrently": "^3.5.1",
"gulp": "^3.9.1",
"gulp-concat": "^2.6.1",
"gulp-cssmin": "^0.2.0",
"gulp-htmlclean": "^2.7.20",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^3.0.0",
"json-server": "^0.12.2",
"rimraf": "^2.6.2",
"typescript": "^2.8.3",
"typings": "^2.1.1"
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900