Cart Licenses Contact Support Online Creator Log in/Register
Try
Buy
v 1.9.44

Show text question answers as chart

loading...

                        


        
            var json = {
  "elements": [
    {
      "type": "text",
      "inputType": "text",
      "name": "zipcode",
      "title": "Postal code"
    }
  ]
};
        

function CustomVisualizer(question, data, options) {
  var values = [];

  var dataProvider = new SurveyAnalytics.DataProvider(data);

  dataProvider.getDataCore = function (visualizer) {
    var result = {};
    this.filteredData.forEach(function (row) {
      var rowValue = row[visualizer.question.name];
      if (rowValue !== undefined) {
        if(result[rowValue] === undefined) {
          result[rowValue] = 1;
        } else {
          result[rowValue]++;
        }
      }
    });

    values.push.apply(values, Object.keys(result));
    return [values.map(function(value) { return result[value]; })];
  };

  options.dataProvider = dataProvider;
  var visualizer = new SurveyAnalytics.SelectBasePlotly(
    question,
    data,
    options,
    "textChartVisualizer"
  );
  visualizer.getValues = function () { return values; };
  visualizer.getLabels = function () { return values; };

  return visualizer;
}

// Unregister other visualizers for the "text" question type
SurveyAnalytics.VisualizationManager.unregisterVisualizer(
  "text",
  SurveyAnalytics.WordCloud
);
SurveyAnalytics.VisualizationManager.unregisterVisualizer(
  "text",
  SurveyAnalytics.Text
);
// Register custom visualizer for the given question type
SurveyAnalytics.VisualizationManager.registerVisualizer(
  "text",
  CustomVisualizer
);
// Set localized title of this visualizer
SurveyAnalytics.localization.locales["en"]["visualizer_textChartVisualizer"] =
  "Text as Chart";

var survey = new Survey.Model(json);
var allQuestions = survey.getAllQuestions();

var panelNode = document.getElementById("vizPanel");
panelNode.innerHTML = "";

var data = [
  { zipcode: "000001" }, { zipcode: "100001" }, { zipcode: "500001" }, { zipcode: "000001" }, { zipcode: "500001" }, { zipcode: "000001" }
];

var options = {
    labelTruncateLength: 27,
    allowTopNAnswers: true,
    hideEmptyAnswers: true,
    answersOrder: 'desc',
    showPercentages: true
};

var visPanel = new SurveyAnalytics.VisualizationPanel(
    allQuestions,
    data,
    options
);
visPanel.showHeader = true;
visPanel.render(panelNode);
$("#loadingIndicator").hide();

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Show text question answers as chart, Reactjs Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/jquery"></script>
    <script src="https://unpkg.com/react@17.0.1/umd/react.production.min.js"></script>
    <script src="https://unpkg.com/react-dom@17.0.1/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.core.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
    <script src="/DevBuilds/survey-react-ui/survey-react-ui.min.js"></script>
    <link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">
<script src="https://cdn.rawgit.com/inexorabletash/polyfill/master/typedarray.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js"></script>

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

<script src="https://unpkg.com/wordcloud@1.1.0/src/wordcloud2.js"></script>        <link href="/DevBuilds/survey-analytics/survey.analytics.min.css" rel="stylesheet" />
        <script src="/DevBuilds/survey-analytics/survey.analytics.min.js"></script>

</head>
<body style="margin: 0">
    
<div id="loadingIndicator">
    <span>
        <div id="loading"><strong>loading...</strong><span></span></div>
    </span>
</div>
<div id="vizPanel"></div>
            <div id="surveyElement" style="display:inline-block;width:100%;">
            </div>
    <div id="surveyResult"></div>

<script type="text/babel" src="./index.js"></script>

</body>
</html>

Tell Us What You Think

Help us serve you better by taking this brief survey.
We are interested to learn more about your
experience of using our libraries.

We'd really appreciate your feedback.

Start the Survey

Approximate time to complete: 2 min.